2 * Copyright (C) 2013-2014 Intel Corporation.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * José Bollo <jose.bollo@open.eurogiciel.org>
20 * Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
21 * Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
27 /* null offset value */
28 #define HNULL ((size_t)-1)
30 /* structure defining the heap */
32 char *data; /* pointer to data of the heap */
33 size_t size; /* count of used byte of the heap */
34 size_t capacity; /* count of byte of the heap */
38 Return the address of the 'heap' memory of 'offset'.
39 The returned pointer is only valid until
40 a call to 'heap_destroy', 'heap_resize', 'heap_alloc'
42 inline static void *heap_address( struct heap *heap, size_t offset)
44 return heap->data + offset;
49 Initialize the 'heap' with a 'capacity' in byte.
50 The allocated size will be zero.
51 Returns 0 if success, -1 if error occured (see then errno)
53 int heap_create( struct heap *heap, size_t capacity);
56 Resize the 'heap' to 'size'.
57 Returns 0 if success, -1 if error occured (see then errno)
59 int heap_resize( struct heap *heap, size_t size);
62 Allocation of 'count' bytes from the 'heap'.
63 Returns the offset (in byte) from the start
64 of the heap. Return HNULL in case of error (see then errno).
66 size_t heap_alloc( struct heap *heap, size_t count);
69 Copy the 'string' of 'length' in the 'heap', appending a terminating null.
70 Return the offset or HNULL if error.
72 size_t heap_strndup( struct heap *heap, const char *string, size_t length);
75 Copy the 'string' in the 'heap' with the terminating null.
76 Return the offset or HNULL if error.
78 size_t heap_strdup( struct heap *heap, const char *string);
82 Returns 0 if success, -1 if error occured (see then errno)
84 int heap_destroy( struct heap *heap);
87 Set the heap as read/write
88 Returns 0 if success, -1 if error occured (see then errno)
90 int heap_read_write( struct heap *heap);
93 Set the heap as read only
94 Returns 0 if success, -1 if error occured (see then errno)
96 int heap_read_only( struct heap *heap);