dbmf.h
A library to manage storage that is allocated and quickly freed.
Database Macro/Free describes a facility that prevents memory fragmentation when temporary storage is being allocated and freed a short time later, at the same time that much longer-lived storage is also being allocated, such as when parsing items for the IOC database while also creating records.
- Author
Jim Kowalkowski, Marty Kraimer
Routines whin iocCore like dbLoadDatabase() have the following attributes:
They repeatedly call malloc() followed soon afterwards by a call to free() the temporarily allocated storage.
Between those calls to malloc() and free(), additional calls to malloc() are made that do NOT have an associated free().
Note
In some environment, e.g. vxWorks, this behavior causes severe memory fragmentation.
Note
This facility should NOT be used by code that allocates storage and then keeps it for a considerable period of time before releasing. Such code should consider using the freeList library.
Functions
-
int dbmfInit(size_t size, int chunkItems)
Initialize the facility.
Note
If dbmfInit() is not called before one of the other routines then it is automatically called with size=64 and chunkItems=10
- Parameters:
size – The maximum size request from dbmfMalloc() that will be allocated from the dbmf pool (Size is always made a multiple of 8).
chunkItems – Each time malloc() must be called size*chunkItems bytes are allocated.
- Returns:
0 on success, -1 if already initialized
-
void *dbmfMalloc(size_t bytes)
Allocate memory.
- Parameters:
bytes – If bytes > size then malloc() is used to allocate the memory.
- Returns:
Pointer to the newly-allocated memory, or NULL on failure.
-
char *dbmfStrdup(const char *str)
Duplicate a string.
Create a copy of the input string.
- Parameters:
str – Pointer to the null-terminated string to be copied.
- Returns:
A pointer to the new copy, or NULL on failure.
-
char *dbmfStrndup(const char *str, size_t len)
Duplicate a string (up to len bytes).
Copy at most len bytes of the input string into a new buffer. If no nil terminator is seen in the first
len
bytes a nil terminator is added.- Parameters:
str – Pointer to the null-terminated string to be copied.
len – Max number of bytes to copy.
- Returns:
A pointer to the new string, or NULL on failure.
-
char *dbmfStrcat3(const char *lhs, const char *mid, const char *rhs)
Concatenate three strings.
Returns a pointer to a null-terminated string made by concatenating the three input strings.
- Parameters:
lhs – Start string to which the others get concatenated to (left part).
mid – Next string to be concatenated to the lhs (mid part).
rhs – Last string to be concatenated to the lhs+mid (right part).
- Returns:
A pointer to the new string, or NULL on failure.
-
void dbmfFree(void *bytes)
Free the memory allocated by dbmfMalloc.
- Parameters:
bytes – Pointer to memory obtained from dbmfMalloc(), dbmfStrdup(), dbmfStrndup() or dbmfStrcat3().
-
void dbmfFreeChunks(void)
Free all chunks that contain only free items.
-
int dbmfShow(int level)
Show the status of the dbmf memory pool.
- Parameters:
level – Detail level.
- Returns:
0.