Submitting rmem memory and object pool module (#898)

* Submitting rmem memory and object pool module

* changed 'restrict' to '__restrict' so it can compile for MSVC

Added `const` to parameters for `MemPool_Realloc`

* Update and rename mempool README.txt to mempool_README.md

* Update mempool_README.md

* Update mempool_README.md

* Update and rename objpool README.txt to objpool_README.md

* implementing changes

* updating header for changes.

* forgot to change _RemoveNode to __RemoveNode

* removing l

* removing l

* Updating documentation on MemPool_CleanUp function

* Updating documentation on ObjPool_CleanUp function

* changed *_CleanUp function parameter

Replaced `void*` pointer to pointer param to `void**` so it's more explicit.

* Updating header to reflect changes to the *_CleanUp functions

* A single change for the mempool and a patch for the objpool.

Object Pool Patch: if you deplete the object pool to 0 free blocks and then free back one block, the last given block will be rejected because it was exactly at the memory holding the entire pool.
Mempool change: switched memory aligning the size from the constructor to when allocating.
This commit is contained in:
Kevin Yonan
2019-07-15 09:28:09 -07:00
committed by Ray
parent e19616592d
commit c563b53afb
4 changed files with 751 additions and 0 deletions

75
src/rmem.h Normal file
View File

@@ -0,0 +1,75 @@
#ifndef RAYLIB_MEMORY_INCLUDED
# define RAYLIB_MEMORY_INCLUDED
#include <stdlib.h>
#include <inttypes.h>
#include <stdbool.h>
#include <string.h>
/************* Memory Pool (mempool.c) *************/
typedef struct MemNode {
size_t size;
struct MemNode *next, *prev;
} MemNode;
typedef struct AllocList {
struct MemNode *head, *tail;
size_t len, maxNodes;
bool autoDefrag : 1;
} AllocList;
typedef struct Stack {
uint8_t *mem, *base;
size_t size;
} Stack;
typedef struct MemPool {
struct AllocList freeList;
struct Stack stack;
} MemPool;
/***************************************************/
/************* Object Pool *************/
typedef struct ObjPool {
struct Stack stack;
size_t objSize, freeBlocks;
} ObjPool;
/***************************************************/
#ifdef __cplusplus
extern "C" {
#endif
/************* Memory Pool *************/
struct MemPool MemPool_Create(size_t bytes);
struct MemPool MemPool_FromBuffer(void *buf, size_t bytes);
void MemPool_Destroy(struct MemPool *mempool);
void *MemPool_Alloc(struct MemPool *mempool, size_t bytes);
void *MemPool_Realloc(struct MemPool *mempool, void *ptr, size_t bytes);
void MemPool_Free(struct MemPool *mempool, void *ptr);
void MemPool_CleanUp(struct MemPool *mempool, void **ptrref);
size_t MemPool_MemoryRemaining(const struct MemPool mempool);
bool MemPool_DeFrag(struct MemPool *mempool);
void MemPool_ToggleAutoDefrag(struct MemPool *mempool);
/***************************************************/
/************* Object Pool (objpool.c) *************/
struct ObjPool ObjPool_Create(size_t objsize, size_t len);
struct ObjPool ObjPool_FromBuffer(void *buf, size_t objsize, size_t len);
void ObjPool_Destroy(struct ObjPool *objpool);
void *ObjPool_Alloc(struct ObjPool *objpool);
void ObjPool_Free(struct ObjPool *objpool, void *ptr);
void ObjPool_CleanUp(struct ObjPool *objpool, void **ptrref);
/***************************************************/
#ifdef __cplusplus
}
#endif
#endif /* RAYLIB_MEMORY_INCLUDED */