Leptonica 1.68
C Image Processing Library

pixalloc.c File Reference

Custom memory allocation for Pix memory. More...

#include "allheaders.h"

Go to the source code of this file.

Data Structures

struct  PixMemoryStore

Typedefs

typedef struct PixMemoryStore L_PIX_MEM_STORE

Functions

l_int32 pmsCreate (size_t minsize, size_t smallest, NUMA *numalloc, const char *logfile)
void pmsDestroy ()
void * pmsCustomAlloc (size_t nbytes)
void pmsCustomDealloc (void *data)
void * pmsGetAlloc (size_t nbytes)
l_int32 pmsGetLevelForAlloc (size_t nbytes, l_int32 *plevel)
l_int32 pmsGetLevelForDealloc (void *data, l_int32 *plevel)
void pmsLogInfo ()

Variables

static L_PIX_MEM_STORECustomPMS = NULL

Detailed Description

Custom memory allocation for Pix memory.

    Custom memory storage with allocator and deallocator

        l_int32       pmsCreate()
        void          pmsDestroy()
        void         *pmsCustomAlloc()
        void          pmsCustomDealloc()
        void         *pmsGetAlloc()
        l_int32       pmsGetLevelForAlloc()
        l_int32       pmsGetLevelForDealloc()
        void          pmsLogInfo()

Definition in file pixalloc.c.


Typedef Documentation

Definition at line 121 of file pixalloc.c.


Function Documentation

l_int32 pmsCreate ( size_t  minsize,
size_t  smallest,
NUMA numalloc,
const char *  logfile 
)

pmsCreate()

Input: minsize (of data chunk that can be supplied by pms) smallest (bytes of the smallest pre-allocated data chunk. numalloc (array with the number of data chunks for each size that are in the memory store) logfile (use for debugging; null otherwise) Return: 0 if OK, 1 on error

Notes: (1) This computes the size of the block of memory required and allocates it. Each chunk starts on a 32-bit word boundary. The chunk sizes are in powers of 2, starting at , and the number of levels and chunks at each level is specified by . (2) This is intended to manage the image data for a small number of relatively large pix. The system malloc is expected to handle very large numbers of small chunks efficiently. (3) Important: set the allocators and call this function before any pix have been allocated. Destroy all the pix in the normal way before calling pmsDestroy(). (4) The pms struct is stored in a static global, so this function is not thread-safe. When used, there must be only one thread per process.

Definition at line 153 of file pixalloc.c.

References PixMemoryStore::allocarray, PixMemoryStore::baseptr, CALLOC, ERROR_INT, PixMemoryStore::firstptr, L_WARNING_FLOAT, PixMemoryStore::largest, PixMemoryStore::logfile, PixMemoryStore::maxptr, PixMemoryStore::memempty, PixMemoryStore::meminuse, PixMemoryStore::memmax, PixMemoryStore::memused, PixMemoryStore::minsize, PixMemoryStore::nbytes, PixMemoryStore::nlevels, nlevels, NULL, numaGetCount(), numaGetIArray(), numaGetSum(), PixMemoryStore::paa, PROCNAME, ptraaCreate(), ptraAdd(), ptraaInsertPtra(), ptraCreate(), PixMemoryStore::sizes, sizes, PixMemoryStore::smallest, and stringNew().

Referenced by main().

void pmsDestroy ( )

pmsDestroy()

Input: (none) Return: void

Notes: (1) Important: call this function at the end of the program, after the last pix has been destroyed.

Definition at line 250 of file pixalloc.c.

References PixMemoryStore::allocarray, PixMemoryStore::baseptr, FALSE, PixMemoryStore::firstptr, FREE, PixMemoryStore::logfile, PixMemoryStore::memempty, PixMemoryStore::meminuse, PixMemoryStore::memmax, PixMemoryStore::memused, NULL, PixMemoryStore::paa, pmsLogInfo(), ptraaDestroy(), and PixMemoryStore::sizes.

Referenced by main().

void* pmsCustomAlloc ( size_t  nbytes)

pmsCustomAlloc()

Input: nbytes (min number of bytes in the chunk to be retrieved) Return: data (ptr to chunk)

Notes: (1) This attempts to find a suitable pre-allocated chunk. If not found, it dynamically allocates the chunk. (2) If logging is turned on, the allocations that are not taken from the memory store, and are at least as large as the minimum size the store can handle, are logged to file.

Definition at line 292 of file pixalloc.c.

References ERROR_PTR, L_HANDLE_ONLY, PixMemoryStore::logfile, PixMemoryStore::memempty, PixMemoryStore::meminuse, PixMemoryStore::memmax, PixMemoryStore::memused, NULL, PixMemoryStore::paa, pmsGetAlloc(), pmsGetLevelForAlloc(), PROCNAME, ptraaGetPtra(), and ptraRemoveLast().

Referenced by main().

void pmsCustomDealloc ( void *  data)

pmsCustomDealloc()

Input: data (to be freed or returned to the storage) Return: void

Definition at line 337 of file pixalloc.c.

References FREE, L_ERROR, L_HANDLE_ONLY, PixMemoryStore::logfile, PixMemoryStore::meminuse, NULL, PixMemoryStore::paa, pmsGetLevelForDealloc(), PROCNAME, ptraAdd(), and ptraaGetPtra().

Referenced by main().

void* pmsGetAlloc ( size_t  nbytes)

pmsGetAlloc()

Input: nbytes Return: data

Notes: (1) This is called when a request for pix data cannot be obtained from the preallocated memory store. After use it is freed like normal memory. (2) If logging is on, only write out allocs that are as large as the minimum size handled by the memory store.

Definition at line 382 of file pixalloc.c.

References CALLOC, ERROR_PTR, fopenWriteStream(), PixMemoryStore::logfile, NULL, PROCNAME, and PixMemoryStore::smallest.

Referenced by pmsCustomAlloc().

l_int32 pmsGetLevelForAlloc ( size_t  nbytes,
l_int32 plevel 
)

pmsGetLevelForAlloc()

Input: nbytes (min number of bytes in the chunk to be retrieved) &level (<return>; -1 if either too small or too large) Return: 0 if OK, 1 on error

Definition at line 413 of file pixalloc.c.

References ERROR_INT, PixMemoryStore::largest, PixMemoryStore::minsize, PixMemoryStore::nlevels, NULL, PROCNAME, and PixMemoryStore::smallest.

Referenced by pmsCustomAlloc().

l_int32 pmsGetLevelForDealloc ( void *  data,
l_int32 plevel 
)

pmsGetLevelForDealloc()

Input: data (ptr to memory chunk) &level (<return> level in memory store; -1 if allocated outside the store) Return: 0 if OK, 1 on error

Definition at line 452 of file pixalloc.c.

References PixMemoryStore::baseptr, ERROR_INT, PixMemoryStore::firstptr, PixMemoryStore::maxptr, PixMemoryStore::nlevels, NULL, and PROCNAME.

Referenced by pmsCustomDealloc().

void pmsLogInfo ( )

Variable Documentation

L_PIX_MEM_STORE* CustomPMS = NULL [static]

Definition at line 123 of file pixalloc.c.

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines