Sha256: b423774faf5464987e93b80d247e0f793d633da4e07a6940cf261c15091ea9f5

Contents?: true

Size: 1.13 KB

Versions: 8

Compression:

Stored size: 1.13 KB

Contents

#ifndef _MEMORY_H_
#define _MEMORY_H_

#include <cstdlib>

/**
 * Pre-allocate a chunk of memory and allocate them in small pieces.
 * Those memory are never freed after allocation. Used for persist
 * data like dictionary contents that will never be destroyed unless
 * the application exited.
 */

namespace rmmseg
{
    const int REALLOC_SIZE = 2048; /* 2KB */

    extern int   _pool_size;
    extern char *_pool_base;

    inline void *pool_alloc(int len)
    {
        void *mem = _pool_base;
        
        if (len <= _pool_size)
        {
            _pool_size -= len;
            _pool_base += len;
            return mem;
        }
        
        /* NOTE: the remaining memory is simply discard, which WILL
         * cause memory leak. However, this function is not for allocating
         * large object. Larger pre-alloc chunk size will also reduce the
         * impact of this leak. So this is generally not a problem. */
        _pool_base = static_cast<char *>(std::malloc(REALLOC_SIZE));
        mem = _pool_base;
        _pool_base += len;
        _pool_size = REALLOC_SIZE - len;
        return mem;
    }
}

#endif /* _MEMORY_H_ */

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
pluskid-rmmseg-cpp-0.2.0 ext/rmmseg/memory.h
pluskid-rmmseg-cpp-0.2.1 ext/rmmseg/memory.h
pluskid-rmmseg-cpp-0.2.2 ext/rmmseg/memory.h
pluskid-rmmseg-cpp-0.2.3 ext/rmmseg/memory.h
pluskid-rmmseg-cpp-0.2.4 ext/rmmseg/memory.h
rmmseg-cpp-0.2.5 ext/rmmseg/memory.h
rmmseg-cpp-0.2.6 ext/rmmseg/memory.h
rmmseg-cpp-0.2.7 ext/rmmseg/memory.h