3. Nibble Memory

Sometimes when you need to allocate and construct a large, interconnected complex of structures, it can be a bit of a pain to release the associated memory again. For the structures describing the Z39.50 PDUs and related structures, it is convenient to use the memory-management system of the ODR subsystem (see Section 2, “Using ODR”). However, in some circumstances where you might otherwise benefit from using a simple nibble-memory management system, it may be impractical to use odr_malloc() and odr_reset(). For this purpose, the memory manager which also supports the ODR streams is made available in the NMEM module. The external interface to this module is given in the nmem.h file.

The following prototypes are given:

    NMEM nmem_create(void);
    void nmem_destroy(NMEM n);
    void *nmem_malloc(NMEM n, size_t size);
    void nmem_reset(NMEM n);
    size_t nmem_total(NMEM n);
    void nmem_init(void);
    void nmem_exit(void);
   

The nmem_create() function returns a pointer to a memory control handle, which can be released again by nmem_destroy() when no longer needed. The function nmem_malloc() allocates a block of memory of the requested size. A call to nmem_reset() or nmem_destroy() will release all memory allocated on the handle since it was created (or since the last call to nmem_reset(). The function nmem_total() returns the number of bytes currently allocated on the handle.

The nibble-memory pool is shared amongst threads. POSIX mutexes and WIN32 Critical sections are introduced to keep the module thread safe. Function nmem_init() initializes the nibble-memory library and it is called automatically the first time the YAZ.DLL is loaded. YAZ uses function DllMain to achieve this. You should not call nmem_init or nmem_exit unless you're absolute sure what you're doing. Note that in previous YAZ versions you'd have to call nmem_init yourself.