98#define BlockFooter(block,size) \
99 ((size_t *) ((char *) (block)+(size)-2*sizeof(size_t)))
100#define BlockHeader(block) ((size_t *) (block)-1)
101#define BlockSize 4096
102#define BlockThreshold 1024
103#define MaxBlockExponent 16
104#define MaxBlocks ((BlockThreshold/(4*sizeof(size_t)))+MaxBlockExponent+1)
105#define MaxSegments 1024
106#define MemoryGuard ((0xdeadbeef << 31)+0xdeafdeed)
107#define NextBlock(block) ((char *) (block)+SizeOfBlock(block))
108#define NextBlockInList(block) (*(void **) (block))
109#define PreviousBlock(block) ((char *) (block)-(*((size_t *) (block)-2)))
110#define PreviousBlockBit 0x01
111#define PreviousBlockInList(block) (*((void **) (block)+1))
112#define SegmentSize (2*1024*1024)
113#define SizeMask (~0x01)
114#define SizeOfBlock(block) (*BlockHeader(block) & SizeMask)
201#if defined(WIZARDSTOOLKIT_EMBEDDABLE_SUPPORT)
245#define AlignedExtent(size,alignment) \
246 (((size)+((alignment)-1)) & ~((alignment)-1))
257 if ((count == 0) || (quantum != (size/count)))
260 return((
void *) NULL);
267 if ((size == 0) || (extent < size))
268 return((
void *) NULL);
269#if defined(WIZARDSTOOLKIT_HAVE_POSIX_MEMALIGN)
270 if (posix_memalign(&memory,alignment,extent) != 0)
272#elif defined(WIZARDSTOOLKIT_HAVE__ALIGNED_MALLOC)
273 memory=_aligned_malloc(extent,alignment);
279 extent=(size+alignment-1)+
sizeof(
void *);
285 memory=(
void *)
AlignedExtent((
size_t) p+
sizeof(
void *),alignment);
286 *((
void **) memory-1)=p;
294#if defined(WIZARDSTOOLKIT_EMBEDDABLE_SUPPORT)
319static inline size_t AllocationPolicy(
size_t size)
328 assert(size % (4*
sizeof(
size_t)) == 0);
330 return(size/(4*
sizeof(
size_t)));
347static inline void InsertFreeBlock(
void *block,
const size_t i)
357 previous=(
void *) NULL;
358 next=memory_pool.
blocks[i];
359 while ((next != (
void *) NULL) && (
SizeOfBlock(next) < size))
366 if (previous != (
void *) NULL)
369 memory_pool.
blocks[i]=block;
370 if (next != (
void *) NULL)
374static inline void RemoveFreeBlock(
void *block,
const size_t i)
382 if (previous == (
void *) NULL)
383 memory_pool.
blocks[i]=next;
386 if (next != (
void *) NULL)
390static void *AcquireBlock(
size_t size)
401 size=(size_t) (size+
sizeof(
size_t)+6*
sizeof(size_t)-1) & -(4U*
sizeof(size_t));
402 i=AllocationPolicy(size);
403 block=memory_pool.
blocks[i];
404 while ((block != (
void *) NULL) && (
SizeOfBlock(block) < size))
406 if (block == (
void *) NULL)
409 while (memory_pool.
blocks[i] == (
void *) NULL)
411 block=memory_pool.
blocks[i];
413 return((
void *) NULL);
417 RemoveFreeBlock(block,AllocationPolicy(
SizeOfBlock(block)));
429 next=(
char *) block+size;
433 InsertFreeBlock(next,AllocationPolicy(blocksize));
478 length=count*quantum;
479 if ((count == 0) || (quantum != (length/count)))
485 sizeof(*memory_info)));
489 memory_info->
length=length;
494 if (memory_info->
blob != NULL)
507 if (memory_info->
blob != NULL)
530 (write(file,
"",1) == 1))
533 if (memory_info->
blob != NULL)
552 if (memory_info->
blob == NULL)
555 if (memory_info->
blob != NULL)
558 if (memory_info->
blob == NULL)
591#if !defined(WIZARDSTOOLKIT_EMBEDDABLE_SUPPORT)
604 assert(2*
sizeof(
size_t) > (
size_t) (~
SizeMask));
621 memory=AcquireBlock(size == 0 ? 1UL : size);
622 if (memory == (
void *) NULL)
624 if (ExpandHeap(size == 0 ? 1UL : size) !=
WizardFalse)
625 memory=AcquireBlock(size == 0 ? 1UL : size);
663 if ((count == 0) || (quantum != (size/count)))
666 return((
void *) NULL);
709 assert(destination != (
void *) NULL);
710 assert(source != (
const void *) NULL);
711 p=(
const unsigned char *) source;
712 q=(
unsigned char *) destination;
713 if (((q+size) < p) || (q > (p+size)))
716 default:
return(memcpy(destination,source,size));
725 case 0:
return(destination);
727 return(memmove(destination,source,size));
750#if defined(WIZARDSTOOLKIT_EMBEDDABLE_SUPPORT)
771#if defined(WIZARDSTOOLKIT_EMBEDDABLE_SUPPORT)
819 if (segment == (
void *) NULL)
821 if (segment == (
void *) NULL)
824 free_segments=segment_info->
next;
825 segment_info->
mapped=mapped;
826 segment_info->
length=blocksize;
828 segment_info->
bound=(
char *) segment+blocksize;
832 memory_pool.
segments[i+1]=segment_info;
834 size=blocksize-12*
sizeof(size_t);
835 block=(
char *) segment_info->
allocation+4*
sizeof(
size_t);
838 InsertFreeBlock(block,AllocationPolicy(size));
840 assert(block < segment_info->bound);
915 return(memory_info->
blob);
943 if (memory == (
void *) NULL)
944 return((
void *) NULL);
945#if defined(WIZARDSTOOLKIT_HAVE_POSIX_MEMALIGN)
947#elif defined(WIZARDSTOOLKIT_HAVE__ALIGNED_MALLOC)
948 _aligned_free(memory);
982 if (memory_info->
blob != (
void *) NULL)
983 switch (memory_info->
type)
994 memory_info->
blob=NULL;
1011 memory_info->
signature=(~WizardSignature);
1013 return(memory_info);
1041 if (memory == (
void *) NULL)
1042 return((
void *) NULL);
1043#if !defined(WIZARDSTOOLKIT_EMBEDDABLE_SUPPORT)
1046 assert((
SizeOfBlock(memory) % (4*
sizeof(
size_t))) == 0);
1058 RemoveFreeBlock(previous,AllocationPolicy(
SizeOfBlock(previous)));
1072 RemoveFreeBlock(next,AllocationPolicy(
SizeOfBlock(next)));
1078 InsertFreeBlock(memory,AllocationPolicy(
SizeOfBlock(memory)));
1081 return((
void *) NULL);
1116 volatile unsigned char
1117 *p = (
volatile unsigned char *) memory;
1122 assert(memory != (
void *) NULL);
1124 *p++=(
unsigned char) c;
1155#if defined(WIZARDSTOOLKIT_EMBEDDABLE_SUPPORT)
1156static inline void *ResizeBlock(
void *block,
size_t size)
1161 if (block == (
void *) NULL)
1162 return(AcquireBlock(size));
1163 memory=AcquireBlock(size);
1164 if (memory == (
void *) NULL)
1165 return((
void *) NULL);
1167 (void) memcpy(memory,block,size);
1169 (
void) memcpy(memory,block,
SizeOfBlock(block)-
sizeof(
size_t));
1180 if (memory == (
void *) NULL)
1182#if !defined(WIZARDSTOOLKIT_EMBEDDABLE_SUPPORT)
1184 if (block == (
void *) NULL)
1188 block=ResizeBlock(memory,size == 0 ? 1UL : size);
1189 if (block == (
void *) NULL)
1191 if (ExpandHeap(size == 0 ? 1UL : size) ==
WizardFalse)
1196 "memory allocation failed `%s'");
1198 block=ResizeBlock(memory,size == 0 ? 1UL : size);
1199 assert(block != (
void *) NULL);
1237 const size_t quantum)
1243 if ((count == 0) || (quantum != (size/count)))
1247 return((
void *) NULL);
WizardExport WizardBooleanType UnmapBlob(void *map, const size_t length)
WizardExport void * MapBlob(int file, const MapMode mode, const WizardOffsetType offset, const size_t length)
#define ThrowFatalException(severity, tag)
#define WizardAssumeAligned(address)
WizardExport MemoryInfo * AcquireVirtualMemory(const size_t count, const size_t quantum, ExceptionInfo *exception)
WizardExport void * AcquireWizardMemory(const size_t size)
struct _DataSegmentInfo DataSegmentInfo
#define NextBlockInList(block)
#define PreviousBlock(block)
WizardExport void * AcquireAlignedMemory(const size_t count, const size_t quantum)
WizardExport void * GetVirtualMemoryBlob(const MemoryInfo *memory_info)
struct _MemoryPool MemoryPool
WizardExport void DestroyWizardMemory(void)
#define PreviousBlockInList(block)
WizardExport void * RelinquishAlignedMemory(void *memory)
WizardExport void * CopyWizardMemory(void *destination, const void *source, const size_t size)
#define BlockHeader(block)
WizardExport MemoryInfo * RelinquishVirtualMemory(MemoryInfo *memory_info)
WizardExport void SetWizardMemoryMethods(AcquireMemoryHandler acquire_memory_handler, ResizeMemoryHandler resize_memory_handler, DestroyMemoryHandler destroy_memory_handler)
WizardExport void * ResizeWizardMemory(void *memory, const size_t size)
WizardExport void * AcquireQuantumMemory(const size_t count, const size_t quantum)
#define SizeOfBlock(block)
WizardExport void * RelinquishWizardMemory(void *memory)
WizardExport void * ResizeQuantumMemory(void *memory, const size_t count, const size_t quantum)
WizardExport void GetWizardMemoryMethods(AcquireMemoryHandler *acquire_memory_handler, ResizeMemoryHandler *resize_memory_handler, DestroyMemoryHandler *destroy_memory_handler)
#define BlockFooter(block, size)
WizardExport void * ResetWizardMemory(void *memory, int c, const size_t size)
static WizardMemoryMethods memory_methods
struct _WizardMemoryMethods WizardMemoryMethods
#define AlignedExtent(size, alignment)
void *(*)(*) *(* ResizeMemoryHandler)(void *, size_t) wizard_alloc_size(2)
void *(* AcquireMemoryHandler)(size_t) wizard_alloc_size(1)
void *(*)(* DestroyMemoryHandler)(void *)
WizardExport WizardBooleanType AcquireWizardResource(const ResourceType type, const WizardSizeType size)
WizardExport void RelinquishWizardResource(const ResourceType type, const WizardSizeType size)
WizardExport WizardBooleanType RelinquishUniqueFileResource(const char *path, const WizardBooleanType trash)
WizardExport int AcquireUniqueFileResource(const char *path, char *filename, ExceptionInfo *exception)
WizardExport void RelinquishSemaphoreInfo(SemaphoreInfo **semaphore_info)
WizardExport void LockSemaphoreInfo(SemaphoreInfo *semaphore_info)
WizardExport void UnlockSemaphoreInfo(SemaphoreInfo *semaphore_info)
WizardExport void ActivateSemaphoreInfo(SemaphoreInfo **semaphore_info)
struct _DataSegmentInfo * previous
struct _DataSegmentInfo * next
char filename[WizardPathExtent]
DataSegmentInfo * segments[MaxSegments]
void * blocks[MaxBlocks+1]
DataSegmentInfo segment_pool[MaxSegments]
ResizeMemoryHandler resize_memory_handler
DestroyMemoryHandler destroy_memory_handler
AcquireMemoryHandler acquire_memory_handler
WizardPrivate ssize_t GetWizardPageSize(void)