📄 memory.c
字号:
FreeAll(struct Data *scr, uchar type){ struct MemInfo *point; void *prev; if(!scr->MallocKey[type]) return; do { point=scr->MallocKey[type]; /* `point' points to the MemInfo structure! */ prev=(void *)point->prev; scr->Dealloc(scr->MallocKey[type], (point->size&SIZEBITS)+sizeof(struct MemInfo), scr->userdata); } while(scr->MallocKey[type]=prev); if(type==MALLOC_DYNAMIC) InitFree(scr); /* init the memory cache tables to prevent accidents with FPLSEND_FLUSHCACHE */}/********************************************************************** * * InitFree() * * Initialize the memory cache/queue. * ******/void REGARGS InitFree(struct Data *scr){ memset(scr->blox, 0, sizeof(struct FreeBlock *)*BLOCK_ENTRIES); memset(scr->blockcount, 0, sizeof(long)*BLOCK_ENTRIES);}/********************************************************************** * * FlushFree(); * * Flush the memory chache. * *******/void REGARGS FlushFree(struct Data *scr){ register long i; for(i=0; i<BLOCK_ENTRIES;i++) { register struct FreeBlock *pnt; while(pnt=scr->blox[i]) { scr->blox[i]=pnt->next; /* delete block from chain */ scr->blockcount[i]--; /* count down counter */ /* free block for real */ Free(scr, (uchar *)pnt+sizeof(struct MemInfo), MALLOC_DYNAMIC); } }}/********************************************************************** * * SwapMem(); * * If type is MALLOC_STATIC or MALLOC_DYNAMIC, this function will secure that * the memory area will be of that kind! * * THE AREA MUST BE ALLOCATED WHEN THIS CONVERTION HAPPENS!!! IT MUST NOT * BE IN THE FREE MEMORY CACHE/QUEUE LIST. * * This function is mainly used when storing global data. Since all * MALLOC_DYNAMIC data gets freed at the end of a FPL program run, we must * convert the mallocs to be able to keep them for next execution! * *****/void REGARGS SwapMem(struct Data *scr, void *ptr, uchar type){ struct MemInfo *point; if(ptr) { /* points to something! */ point=(struct MemInfo *)((uchar *)ptr-sizeof(struct MemInfo)); /* `point' points to the MemInfo structure: */ if(point->size&ALLOCBIT && type==MALLOC_DYNAMIC) { /* This is a MALLOC_STATIC alloc */ UnLinkMemory(scr, point, MALLOC_STATIC); /* take away from static list */ point->size&=SIZEBITS; LinkMemory(scr, point, MALLOC_DYNAMIC); /* insert in dynamic list */ } else if(!(point->size&ALLOCBIT) && type==MALLOC_STATIC) { /* This is a MALLOC_DYNAMIC alloc */ UnLinkMemory(scr, point, MALLOC_DYNAMIC); /* take away from dynamic list */ point->size|=ALLOCBIT; LinkMemory(scr, point, MALLOC_STATIC); /* insert in static list */ } /* If no if() statement was reached, the memory area type was corect! */ }}/********************************************************************** * * TypeMem() * * Returns the type of the specified memory pointer. * Returns MALLOC_STATIC or MALLOC_DYNAMIC. * ******************************************/uchar REGARGS TypeMem(void *ptr){ struct MemInfo *point=(struct MemInfo *)((uchar *)ptr-sizeof(struct MemInfo)); /* `point' points to the MemInfo structure: */ if(point->size&ALLOCBIT) return MALLOC_STATIC; return MALLOC_DYNAMIC;}/********************************************************************** * * fplAlloc() * * fplAlloc() allocates memory and assign it to the fpl internal list of * used memory. If fplFree() is called, this memory area will be free'd if * not earlier. Free this memory using fplDealloc(). * ******/void PREFIX *fplAlloc(AREG(0) struct Data *scr, DREG(0) long size){#ifdef DEBUGMAIL DebugMail(scr, MAIL_FUNCTION, 500, "fplAlloc");#endif if(!scr) return(NULL); return MALLOCA(size);}/********************************************************************** * * fplDealloc() * * This function frees memory previously allocated with fplAlloc(); * *****/void PREFIX fplDealloc(AREG(0) struct Data *scr, AREG(1) void *ptr){#ifdef DEBUGMAIL DebugMail(scr, MAIL_FUNCTION, 500, "fplDealloc");#endif if(!scr) return; FREE_KIND(ptr);}/********************************************************************** * * fplAlloca() * * fplAlloca() allocates memory and assign it to the fpl internal list of * used memory. The memory will only exist while executing the current file or * until next program has been executed. If fplFree() is called, this memory * area will also be free'd. Free this memory using fplDealloca(). This function * will use the FPL internal memory caching system. * ******/void PREFIX *fplAlloca(AREG(0) struct Data *scr, DREG(0) long size){#ifdef DEBUGMAIL DebugMail(scr, MAIL_FUNCTION, 500, "fplAlloca");#endif if(!scr) return(NULL); return MALLOC(size);}/********************************************************************** * * fplDealloca() * * This function frees memory previously allocated with fplAlloca(); * *****/void PREFIX fplDealloca(AREG(0) struct Data *scr, AREG(1) void *ptr){ fplDealloc(scr, ptr);}/*************************************************************** * * fplAllocString() * * Returns a pointer to a memory block of the requested size. * The memory block is initialized as a string and can be used * when returning strings to FPL with fplSend(), without having * to allocate memory, make FPL have to allocate memory and copy * all your data and then you free your memory again. * Now all you have to do is allocate a string at once and then * tell FPL in fplSend() using the FPLSEND_DONTCOPY_STRING. The memory * block will then be used as it is by FPL. * * If you want to free the memory, you do that with fplFreeString(). * You must not free memory sent to FPL! * ********/void PREFIX *fplAllocString(AREG(0) struct Data *scr, DREG(0) long size){ uchar *ptr;#ifdef DEBUGMAIL DebugMail(scr, MAIL_FUNCTION, 500, "fplAllocString");#endif /* * Allocate the memory DYNAMIC and convert it to staic later so that the * memory block gets aligned properly and then later can get converted to * DYNAMIC kind again without any risking anything! */ ptr = MALLOC(size + sizeof(struct fplStr)); if(!ptr) return NULL; /* * Convert this to static, most strings allocated like this are likely to * be used as "static" memory areas! */ SwapMem(scr, ptr, MALLOC_STATIC); ((struct fplStr *)ptr)->alloc=size; ((struct fplStr *)ptr)->len=size; /* set default to entire string */ ptr+=offsetof(struct fplStr, string); /* make 'ptr' point to the string entry */ return ptr; /* return string must be freed with fplFreeString() */}void PREFIX fplFreeString(AREG(0) struct Data *scr, AREG(1) void *ptr){ uchar *point=ptr;#ifdef DEBUGMAIL DebugMail(scr, MAIL_FUNCTION, 500, "fplFreeString");#endif if(!point) return; point-=offsetof(struct fplStr, string); /* go to beginning of allocation */ FREEA(point); /* free the string! */}/********************************************************************** * * FreeKind() * * Frees the memory area, of both dynamic and static kinds! * ****************/void REGARGS FreeKind(struct Data *scr, void *memory){ if(MALLOC_DYNAMIC == TypeMem(memory)) { FREE(memory); } else { FREEA(memory); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -