view projects/heavy/circularBuffer/MessagePool.h @ 163:20b52283c7b4 heavy-updated

- added circular buffer pd/heavy example (works but process needs to be killed manually if launched via ssh?)
author chnrx <chris.heinrichs@gmail.com>
date Thu, 12 Nov 2015 15:55:30 +0000
parents
children
line wrap: on
line source
/**
 * Copyright (c) 2014, 2015, Enzien Audio Ltd.
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef _MESSAGE_POOL_H_
#define _MESSAGE_POOL_H_

#include "Utils.h"

struct HvMessage;

#define MP_NUM_MESSAGE_LISTS 4

typedef struct MessagePoolList {
  struct MessageListNode *head; // list of currently available blocks
  struct MessageListNode *pool; // list of currently used blocks
} MessagePoolList;

typedef struct MessagePool {
  char *buffer; // the buffer of all messages
  hv_size_t bufferSize; // in bytes
  hv_size_t bufferIndex; // the number of total reserved bytes

  MessagePoolList lists[MP_NUM_MESSAGE_LISTS];
} MessagePool;

/**
 * The MessagePool is a basic memory management system. It reserves a large block of memory at initialisation
 * and proceeds to divide this block into smaller chunks (usually 512 bytes) as they are needed. These chunks are
 * further divided into 32, 64, 128, or 256 sections. Each of these sections is managed by a MessagePoolList (MPL).
 * An MPL is a linked-list data structure which is initialised such that its own pool of listnodes is filled with nodes
 * that point at each subblock (e.g. each 32-byte block of a 512-block chunk).
 *
 * MessagePool is loosely inspired by TCMalloc. http://goog-perftools.sourceforge.net/doc/tcmalloc.html
 */

hv_size_t mp_init(struct MessagePool *mp, hv_size_t numKB);

void mp_free(struct MessagePool *mp);

/**
 * Adds a message to the pool and returns a pointer to the copy. Returns NULL
 * if no space was available in the pool.
 */
struct HvMessage *mp_addMessage(struct MessagePool *mp, const struct HvMessage *m);

void mp_freeMessage(struct MessagePool *mp, struct HvMessage *m);

#endif // _MESSAGE_POOL_H_