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