-/* tool memory helper ------------------------------------------------------- */
-
-/*
- * UToolMemory is used for generic, custom memory management.
- * It is allocated with enough space for count*size bytes starting
- * at array.
- * The array is declared with a union of large data types so
- * that its base address is aligned for any types.
- * If size is a multiple of a data type size, then such items
- * can be safely allocated inside the array, at offsets that
- * are themselves multiples of size.
- */
-typedef struct UToolMemory {
- char name[64];
- uint32_t count, size, index;
- union {
- uint32_t u;
- double d;
- void *p;
- } array[1];
-} UToolMemory;
-
-static UToolMemory *
-utm_open(const char *name, uint32_t count, uint32_t size) {
- UToolMemory *mem=(UToolMemory *)uprv_malloc(sizeof(UToolMemory)+count*size);
- if(mem==NULL) {
- fprintf(stderr, "error: %s - out of memory\n", name);
- exit(U_MEMORY_ALLOCATION_ERROR);
- }
- uprv_strcpy(mem->name, name);
- mem->count=count;
- mem->size=size;
- mem->index=0;
- return mem;
-}
-
-static void
-utm_close(UToolMemory *mem) {
- if(mem!=NULL) {
- uprv_free(mem);
- }
-}
-
-
-
-static void *
-utm_getStart(UToolMemory *mem) {
- return (char *)mem->array;
-}
-
-static int32_t
-utm_countItems(UToolMemory *mem) {
- return mem->index;
-}
-
-static void *
-utm_alloc(UToolMemory *mem) {
- char *p=(char *)mem->array+mem->index*mem->size;
- if(++mem->index<=mem->count) {
- uprv_memset(p, 0, mem->size);
- return p;
- } else {
- fprintf(stderr, "error: %s - trying to use more than %ld preallocated units\n",
- mem->name, (long)mem->count);
- exit(U_MEMORY_ALLOCATION_ERROR);
- }
-}
-
-static void *
-utm_allocN(UToolMemory *mem, int32_t n) {
- char *p=(char *)mem->array+mem->index*mem->size;
- if((mem->index+=(uint32_t)n)<=mem->count) {
- uprv_memset(p, 0, n*mem->size);
- return p;
- } else {
- fprintf(stderr, "error: %s - trying to use more than %ld preallocated units\n",
- mem->name, (long)mem->count);
- exit(U_MEMORY_ALLOCATION_ERROR);
- }
-}
-