memory_pool.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2014 Swift Navigation Inc.
00003  * Contact: Fergus Noble <fergus@swift-nav.com>
00004  *
00005  * This source is subject to the license found in the file 'LICENSE' which must
00006  * be be distributed together with this source. All other rights reserved.
00007  *
00008  * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
00009  * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
00010  * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
00011  */
00012 
00013 #ifndef LIBSWIFTNAV_MEMORY_POOL_H
00014 #define LIBSWIFTNAV_MEMORY_POOL_H
00015 
00016 #include <stddef.h>
00017 
00018 #include "common.h"
00019 
00020 /* Type for elements of the memory pool, unfortunately typedef doesn't enforce
00021  * type safety and an opaque struct definition wouldn't be compatible with the
00022  * flexible member array in node_t. */
00023 typedef u8 element_t;
00024 
00025 typedef struct _memory_pool memory_pool_t;
00026 
00027 struct node;
00028 
00029 typedef struct {
00030   struct node *next;
00031 } memory_pool_node_hdr_t;
00032 
00033 typedef struct node {
00034   memory_pool_node_hdr_t hdr;
00035   /* C99 "flexible member array" (see C99 std. ch. 6.7.2.1),
00036    * Allows us to get a pointer to the top of the unknown size element. */
00037   element_t elem[];
00038 } node_t;
00039 
00040 struct _memory_pool {
00041   u32 n_elements;
00042   size_t element_size;
00043   node_t *pool;
00044   node_t *free_nodes_head;
00045   node_t *allocated_nodes_head;
00046 };
00047 
00048 
00049 memory_pool_t *memory_pool_new(u32 n_elements, size_t element_size);
00050 s8 memory_pool_init(memory_pool_t *new_pool, u32 n_elements,
00051                     size_t element_size, void *buff);
00052 void memory_pool_destroy(memory_pool_t *pool);
00053 s32 memory_pool_n_free(memory_pool_t *pool);
00054 s32 memory_pool_n_allocated(memory_pool_t *pool);
00055 u8 memory_pool_empty(memory_pool_t *pool);
00056 u32 memory_pool_n_elements(memory_pool_t *pool);
00057 
00058 element_t *memory_pool_add(memory_pool_t *pool);
00059 s32 memory_pool_to_array(memory_pool_t *pool, void *array);
00060 
00061 s32 memory_pool_map(memory_pool_t *pool, void *arg,
00062                     void (*f)(void *arg, element_t *elem));
00063 s32 memory_pool_filter(memory_pool_t *pool, void *arg,
00064                        s8 (*f)(void *arg, element_t *elem));
00065 s32 memory_pool_clear(memory_pool_t *pool);
00066 s32 memory_pool_fold(memory_pool_t *pool, void *x0,
00067                      void (*f)(void *x, element_t *elem));
00068 double memory_pool_dfold(memory_pool_t *pool, double x0,
00069                          double (*f)(double x, element_t *elem));
00070 float memory_pool_ffold(memory_pool_t *pool, float x0,
00071                         float (*f)(float x, element_t *elem));
00072 s32 memory_pool_ifold(memory_pool_t *pool, s32 x0,
00073                       s32 (*f)(s32 x, element_t *elem));
00074 
00075 void memory_pool_sort(memory_pool_t *pool, void *arg,
00076                       s32 (*cmp)(void *arg, element_t *a, element_t *b));
00077 void memory_pool_group_by(memory_pool_t *pool, void *arg,
00078                           s32 (*cmp)(void *arg, element_t *a, element_t *b),
00079                           void *x0, size_t x_size,
00080                           void (*agg)(element_t *new, void *x, u32 n, element_t *elem));
00081 s32 memory_pool_product(memory_pool_t *pool, void *xs, u32 max_xs, size_t x_size,
00082                         void (*prod)(element_t *new, void *x, u32 n_xs, u32 n, element_t *elem));
00083 s32 memory_pool_product_generator(memory_pool_t *pool, void *x0, u32 n_xs, size_t x_size,
00084                                   s8 (*next)(void *x, u32 n),
00085                                   void (*prod)(element_t *new, void *x, u32 n, element_t *elem));
00086 
00087 #endif /* LIBSWIFTNAV_MEMORY_POOL_H */
00088 


swiftnav
Author(s):
autogenerated on Sat Jun 8 2019 18:55:56