chris@162: /** chris@162: * Copyright (c) 2014, 2015, Enzien Audio Ltd. chris@162: * chris@162: * Permission to use, copy, modify, and/or distribute this software for any chris@162: * purpose with or without fee is hereby granted, provided that the above chris@162: * copyright notice and this permission notice appear in all copies. chris@162: * chris@162: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH chris@162: * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY chris@162: * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, chris@162: * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM chris@162: * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR chris@162: * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR chris@162: * PERFORMANCE OF THIS SOFTWARE. chris@162: */ chris@162: chris@162: #ifndef _MESSAGE_POOL_H_ chris@162: #define _MESSAGE_POOL_H_ chris@162: chris@162: #include "Utils.h" chris@162: chris@162: struct HvMessage; chris@162: chris@162: #define MP_NUM_MESSAGE_LISTS 4 chris@162: chris@162: typedef struct MessagePoolList { chris@162: struct MessageListNode *head; // list of currently available blocks chris@162: struct MessageListNode *pool; // list of currently used blocks chris@162: } MessagePoolList; chris@162: chris@162: typedef struct MessagePool { chris@162: char *buffer; // the buffer of all messages chris@162: hv_size_t bufferSize; // in bytes chris@162: hv_size_t bufferIndex; // the number of total reserved bytes chris@162: chris@162: MessagePoolList lists[MP_NUM_MESSAGE_LISTS]; chris@162: } MessagePool; chris@162: chris@162: /** chris@162: * The MessagePool is a basic memory management system. It reserves a large block of memory at initialisation chris@162: * and proceeds to divide this block into smaller chunks (usually 512 bytes) as they are needed. These chunks are chris@162: * further divided into 32, 64, 128, or 256 sections. Each of these sections is managed by a MessagePoolList (MPL). chris@162: * An MPL is a linked-list data structure which is initialised such that its own pool of listnodes is filled with nodes chris@162: * that point at each subblock (e.g. each 32-byte block of a 512-block chunk). chris@162: * chris@162: * MessagePool is loosely inspired by TCMalloc. http://goog-perftools.sourceforge.net/doc/tcmalloc.html chris@162: */ chris@162: chris@162: hv_size_t mp_init(struct MessagePool *mp, hv_size_t numKB); chris@162: chris@162: void mp_free(struct MessagePool *mp); chris@162: chris@162: /** chris@162: * Adds a message to the pool and returns a pointer to the copy. Returns NULL chris@162: * if no space was available in the pool. chris@162: */ chris@162: struct HvMessage *mp_addMessage(struct MessagePool *mp, const struct HvMessage *m); chris@162: chris@162: void mp_freeMessage(struct MessagePool *mp, struct HvMessage *m); chris@162: chris@162: #endif // _MESSAGE_POOL_H_