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