chris@163
|
1 /**
|
chris@163
|
2 * Copyright (c) 2014, 2015, Enzien Audio Ltd.
|
chris@163
|
3 *
|
chris@163
|
4 * Permission to use, copy, modify, and/or distribute this software for any
|
chris@163
|
5 * purpose with or without fee is hereby granted, provided that the above
|
chris@163
|
6 * copyright notice and this permission notice appear in all copies.
|
chris@163
|
7 *
|
chris@163
|
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
chris@163
|
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
chris@163
|
10 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
chris@163
|
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
chris@163
|
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
chris@163
|
13 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
chris@163
|
14 * PERFORMANCE OF THIS SOFTWARE.
|
chris@163
|
15 */
|
chris@163
|
16
|
chris@163
|
17 #ifndef _MESSAGE_POOL_H_
|
chris@163
|
18 #define _MESSAGE_POOL_H_
|
chris@163
|
19
|
chris@163
|
20 #include "Utils.h"
|
chris@163
|
21
|
chris@163
|
22 struct HvMessage;
|
chris@163
|
23
|
chris@163
|
24 #define MP_NUM_MESSAGE_LISTS 4
|
chris@163
|
25
|
chris@163
|
26 typedef struct MessagePoolList {
|
chris@163
|
27 struct MessageListNode *head; // list of currently available blocks
|
chris@163
|
28 struct MessageListNode *pool; // list of currently used blocks
|
chris@163
|
29 } MessagePoolList;
|
chris@163
|
30
|
chris@163
|
31 typedef struct MessagePool {
|
chris@163
|
32 char *buffer; // the buffer of all messages
|
chris@163
|
33 hv_size_t bufferSize; // in bytes
|
chris@163
|
34 hv_size_t bufferIndex; // the number of total reserved bytes
|
chris@163
|
35
|
chris@163
|
36 MessagePoolList lists[MP_NUM_MESSAGE_LISTS];
|
chris@163
|
37 } MessagePool;
|
chris@163
|
38
|
chris@163
|
39 /**
|
chris@163
|
40 * The MessagePool is a basic memory management system. It reserves a large block of memory at initialisation
|
chris@163
|
41 * and proceeds to divide this block into smaller chunks (usually 512 bytes) as they are needed. These chunks are
|
chris@163
|
42 * further divided into 32, 64, 128, or 256 sections. Each of these sections is managed by a MessagePoolList (MPL).
|
chris@163
|
43 * An MPL is a linked-list data structure which is initialised such that its own pool of listnodes is filled with nodes
|
chris@163
|
44 * that point at each subblock (e.g. each 32-byte block of a 512-block chunk).
|
chris@163
|
45 *
|
chris@163
|
46 * MessagePool is loosely inspired by TCMalloc. http://goog-perftools.sourceforge.net/doc/tcmalloc.html
|
chris@163
|
47 */
|
chris@163
|
48
|
chris@163
|
49 hv_size_t mp_init(struct MessagePool *mp, hv_size_t numKB);
|
chris@163
|
50
|
chris@163
|
51 void mp_free(struct MessagePool *mp);
|
chris@163
|
52
|
chris@163
|
53 /**
|
chris@163
|
54 * Adds a message to the pool and returns a pointer to the copy. Returns NULL
|
chris@163
|
55 * if no space was available in the pool.
|
chris@163
|
56 */
|
chris@163
|
57 struct HvMessage *mp_addMessage(struct MessagePool *mp, const struct HvMessage *m);
|
chris@163
|
58
|
chris@163
|
59 void mp_freeMessage(struct MessagePool *mp, struct HvMessage *m);
|
chris@163
|
60
|
chris@163
|
61 #endif // _MESSAGE_POOL_H_
|