?? htmemory.h
字號(hào):
/* W3C Sample Code Library libwww Dynamic Memory Handlers! Dynamic Memory Handlers!*//*** (c) COPYRIGHT MIT 1995.** Please first read the full copyright statement in the file COPYRIGH.*//*This module defines any memory handler to be used by libwww for allocatingand de-allocating dynamic memory. As dynamic memory may be a scarce resource,it is required that an application can handle memory exhaustion gracefully.This module provides an interface that covers the following situations: o Handling of allocation, reallocation and de-allocation of dynamic memory o Recovering from temporary lack of available memory o Panic handling in case a new allocation fails Note: The Library core provides a default set of memory handlersfor allocating and de-allocating dynamic memory. In order to maintain areasonable performance, they are not registered dynamically but assignedusing C style macros. Hence, it is not possible to swap memory handlerat run time but this was considered to be a reasonable trade-off.This module is implemented by HTMemory.c, and itis a part of the W3C Sample CodeLibrary.*/#ifndef HTMEMORY_H#define HTMEMORY_H#include "HTUtils.h"/*. Allocation, Reallocation and De-allocation.The Library provides a default set of methods for handling dynamic memory.They are very basic and essentially identical to the C stylemalloc, calloc, realloc, andfree:*/extern void* HTMemory_malloc(size_t size);extern void* HTMemory_calloc(size_t count, size_t size);extern void* HTMemory_realloc(void * ptr, size_t size);extern void HTMemory_free(void* ptr);/*( Memory Macros)The methods above are not referred directly in the Library. Instead we usea set of C style macros. If you don't wany any memory management beyond normalmalloc and alloc then you can just use that instead of the HTMemory_* function.You can of course also provide your own methods as well.*/#define HT_MALLOC(size) HTMemory_malloc((size))#define HT_CALLOC(count, size) HTMemory_calloc((count), (size))#define HT_REALLOC(ptr, size) HTMemory_realloc((ptr), (size))#define HT_FREE(pointer) {HTMemory_free((pointer));((pointer))=NULL;}/*. Memory Freer Functions.The dynamic memory freer functions are typically functions that are capableof freeing large chunks of memory. In case a new allocation fails, the allocationmethod looks for any registered freer functions to call. There can be multiplefreer functions and after each call, the allocation method tries again toallocate the desired amount of dynamic memory. The freer functions are calledin reverse order meaning that the last one registered getscalled first. That way, it is easy to add temporary freer functionswhich then are guaranteed to be called first if a methods fails.( Add a Freer Function)You can add a freer function by using the following method. The Library mayitself register a set of free functions during initialization. If the applicationdoes not register any freer functions then the Library looks how it can freeinternal memory. The freer function is passed the total number ofbytes requested by the allocation.*/typedef void HTMemoryCallback(size_t size);extern BOOL HTMemoryCall_add (HTMemoryCallback * cbf);/*( Delete a Freer Function)Freer functions can be deleted at any time in which case they are not calledanymore.*/extern BOOL HTMemoryCall_delete (HTMemoryCallback * cbf);extern BOOL HTMemoryCall_deleteAll (void);/*. Panic Handling.If the freer functions are not capable of de-allocation enough memory thenthe application must have an organized way of closing down. This is doneusing the panic handler. In the libwww, each allocation is tested andHT_OUTOFMEM is called if a NULL was returned.HT_OUTOFMEM is a macro which by default callsHTMemory_outofmem() but of course can point to any method. Thedefault handler calls an exit function defined by the application in a callto HTMemory_setExit(). If the application has not definedan exit function, HTMemory_outofmem() prints an error messageand calls exit(1).*/typedef void HTMemory_exitCallback(char *name, char *file, unsigned long line);extern void HTMemory_setExit(HTMemory_exitCallback * pExit);extern HTMemory_exitCallback * HTMemory_exit(void);/*( Call the Exit Handler)If an allocation fails then this function is called. If the application hasregistered its own panic handler then this is called directly from this function.Otherwise, the default behavior is to write a small message to stderr andthen exit.*/#define outofmem(file, name) HT_OUTOFMEM(name)#define HT_OUTOFMEM(name) HTMemory_outofmem((name), __FILE__, __LINE__)extern void HTMemory_outofmem(char * name, char * file, unsigned long line);/**/#endif /* HTMEMORY_H *//* @(#) $Id: HTMemory.html,v 2.12 1998/05/14 02:10:45 frystyk Exp $*/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -