00001 /******************************************************************************* 00002 / 00003 / File: area_malloc.h 00004 / 00005 / Description: Kernel area_malloc module API 00006 / 00007 / Copyright 1999, Be Incorporated, All Rights Reserved. 00008 / 00009 *******************************************************************************/ 00010 00011 #ifndef _AREA_MALLOC_H_ 00012 #define _AREA_MALLOC_H_ 00013 00014 #include <module.h> 00015 00016 #ifdef __cplusplus 00017 extern "C" { 00018 #endif 00019 00020 /* 00021 area_malloc is a wrapper around the general purpose internal malloc() 00022 routines that arranges for memory to come from areas rather than the 00023 heap. malloc(), calloc(), realloc() and free() behave like their 00024 user-space counterparts, except they require a pool to work from as 00025 their first argument. 00026 00027 Note that these routines are *NOT* safe to call inside an interrupt 00028 handler. They may block on semaphores. 00029 00030 SMP Safety 00031 malloc(), calloc(), realloc() and free() are SMP-safe with respect 00032 to one another, but not in regards to delete_pool(). In other words, 00033 don't call delete_pool() until you know the other routines are not in 00034 use. create_pool() and delete_pool() are safe with respect to each 00035 other. 00036 00037 When the last user of the module puts it away, any remaining pools are 00038 deleted. 00039 00040 const void * create_pool(uint32 address_spec, size_t size, uint32 lock_spec, uint32 protection) 00041 Creates a new pool of memory to allocate from. The parameters are 00042 the same as used for create_area(), so that you have complete 00043 control over the characteristics (if not the name) of the area used 00044 as the memory pool. Returns an opaque pool identifier, or NULL if 00045 the creation failed. The sharability of the resources allocated 00046 from this pool are determined by the permisions and protections used 00047 to create the area. In other words, the rules for sharing areas 00048 apply. It is possible to find out the area_id of the area from 00049 which the memory is allocated, but I'm not going tell you how. 00050 00051 status_t delete_pool(const void *pool_id); 00052 Deletes the specified pool, returning all related resources to the 00053 kernel. All pointers returned from malloc(), calloc() or realloc() 00054 when this pool was specified are immediately invalid. Returns B_OK 00055 if the pool was deleted, or B_ERROR if an invalid pool_id was 00056 passed. 00057 00058 00059 These routines behave exactly like their libroot.so counterparts, except 00060 they operate on a particular pool. 00061 00062 void * malloc(const void *pool_id, size_t size) 00063 Allocates size bytes from the specified pool, or returns NULL if 00064 there is no more available memory. 00065 00066 void * calloc(const void *pool_id, size_t nmemb, size_t size) 00067 Allocates nmemb * size bytes from the pool and initializes the 00068 memory to 0. Returns a pointer to the memory, or NULL if the 00069 allocation failed. 00070 00071 void * realloc(const void *pool_id, void *ptr, size_t size) 00072 Changes the size of the memory block pointed to by ptr to size 00073 bytes. Returns a pointer to the memory, which may be NULL 00074 indicating failure, the same as ptr indicating the memory block 00075 was resized in place, or different indicating that the contents 00076 of the memory block were copied to a new location of the 00077 appropriate size. 00078 00079 void free(const void *pool_id, void *ptr) 00080 Releases the memory pointed at by ptr back to the pool. 00081 00082 */ 00083 00084 #define B_AREA_MALLOC_MODULE_NAME "generic/area_malloc/v1" 00085 00086 typedef struct { 00087 module_info minfo; 00088 const void * (*create_pool)(uint32 address_spec, size_t size, uint32 lock_spec, uint32 protection); 00089 status_t (*delete_pool)(const void *pool_id); 00090 void * (*malloc)(const void *pool_id, size_t size); 00091 void * (*calloc)(const void *pool_id, size_t nmemb, size_t size); 00092 void * (*realloc)(const void *pool_id, void *ptr, size_t size); 00093 void (*free)(const void *pool_id, void *ptr); 00094 } area_malloc_module_info; 00095 00096 #ifdef __cplusplus 00097 } 00098 #endif 00099 00100 #endif 00101