2001-09-07 18:42:29 +00:00
|
|
|
/*
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 1999-2001, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
2001-09-25 16:43:20 +00:00
|
|
|
******************************************************************************/
|
2001-09-07 18:42:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* UDataMemory A class-like struct that serves as a handle to a piece of memory
|
|
|
|
* that contains some ICU data (resource, converters, whatever.)
|
|
|
|
*
|
|
|
|
* When an application opens ICU data (with udata_open, for example,
|
|
|
|
* a UDataMemory * is returned.
|
|
|
|
*
|
|
|
|
*----------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "cmemory.h"
|
|
|
|
#include "unicode/udata.h"
|
|
|
|
|
|
|
|
#include "udatamem.h"
|
|
|
|
|
|
|
|
void UDataMemory_init(UDataMemory *This) {
|
|
|
|
uprv_memset(This, 0, sizeof(UDataMemory));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UDatamemory_assign(UDataMemory *dest, UDataMemory *source) {
|
|
|
|
/* UDataMemory Assignment. Destination UDataMemory must be initialized first. */
|
|
|
|
UBool mallocedFlag = dest->heapAllocated;
|
|
|
|
uprv_memcpy(dest, source, sizeof(UDataMemory));
|
|
|
|
dest->heapAllocated = mallocedFlag;
|
|
|
|
}
|
|
|
|
|
|
|
|
UDataMemory *UDataMemory_createNewInstance(UErrorCode *pErr) {
|
|
|
|
UDataMemory *This;
|
|
|
|
|
|
|
|
if (U_FAILURE(*pErr)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
This = uprv_malloc(sizeof(UDataMemory));
|
|
|
|
if (This == NULL) {
|
|
|
|
*pErr = U_MEMORY_ALLOCATION_ERROR; }
|
|
|
|
else {
|
|
|
|
UDataMemory_init(This);
|
|
|
|
This->heapAllocated = TRUE;
|
|
|
|
}
|
|
|
|
return This;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const DataHeader *
|
2001-10-05 21:48:05 +00:00
|
|
|
UDataMemory_normalizeDataPointer(const void *p) {
|
2001-09-07 18:42:29 +00:00
|
|
|
/* allow the data to be optionally prepended with an alignment-forcing double value */
|
|
|
|
const DataHeader *pdh = (const DataHeader *)p;
|
|
|
|
if(pdh==NULL || (pdh->dataHeader.magic1==0xda && pdh->dataHeader.magic2==0x27)) {
|
|
|
|
return pdh;
|
|
|
|
} else {
|
2003-04-25 00:17:44 +00:00
|
|
|
#ifdef OS400
|
|
|
|
/*
|
2003-05-20 00:13:02 +00:00
|
|
|
TODO: Fix this once the compiler implements this feature. Keep in sync with genccode.c
|
|
|
|
|
|
|
|
This is here because this platform can't currently put
|
2003-04-25 00:17:44 +00:00
|
|
|
const data into the read-only pages of an object or
|
|
|
|
shared library (service program). Only strings are allowed in read-only
|
|
|
|
pages, so we use char * strings to store the data.
|
|
|
|
|
|
|
|
In order to prevent the beginning of the data from ever matching the
|
|
|
|
magic numbers we must skip the initial double.
|
|
|
|
[grhoten 4/24/2003]
|
|
|
|
*/
|
2003-05-20 00:13:02 +00:00
|
|
|
return (const DataHeader *)*((const void **)p+1);
|
2003-04-25 00:17:44 +00:00
|
|
|
#else
|
2001-09-07 18:42:29 +00:00
|
|
|
return (const DataHeader *)((const double *)p+1);
|
2003-04-25 00:17:44 +00:00
|
|
|
#endif
|
2001-09-07 18:42:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UDataMemory_setData (UDataMemory *This, const void *dataAddr) {
|
2001-10-05 21:48:05 +00:00
|
|
|
This->pHeader = UDataMemory_normalizeDataPointer(dataAddr);
|
2001-09-07 18:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
udata_close(UDataMemory *pData) {
|
|
|
|
if(pData!=NULL) {
|
|
|
|
uprv_unmapFile(pData);
|
|
|
|
if(pData->heapAllocated ) {
|
|
|
|
uprv_free(pData);
|
|
|
|
} else {
|
|
|
|
UDataMemory_init(pData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI const void * U_EXPORT2
|
|
|
|
udata_getMemory(UDataMemory *pData) {
|
|
|
|
if(pData!=NULL && pData->pHeader!=NULL) {
|
|
|
|
return (char *)(pData->pHeader)+pData->pHeader->dataHeader.headerSize;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UBool UDataMemory_isLoaded(UDataMemory *This) {
|
|
|
|
return This->pHeader != NULL;
|
|
|
|
}
|
|
|
|
|