| 1 |
/* dctrl-tools - Debian control file inspection tools |
| 2 |
Copyright © 2004, 2012 Antti-Juhani Kaijanaho |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation; either version 2 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program 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 |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License along |
| 15 |
with this program; if not, write to the Free Software Foundation, Inc., |
| 16 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 |
*/ |
| 18 |
|
| 19 |
#ifndef PARA_POOL_H |
| 20 |
#define PARA_POOL_H |
| 21 |
|
| 22 |
#include <stdint.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include "align.h" |
| 25 |
#include "msg.h" |
| 26 |
#include "paragraph.h" |
| 27 |
|
| 28 |
#define NUM_PAGES 1 |
| 29 |
|
| 30 |
struct para_pages { |
| 31 |
size_t max; |
| 32 |
size_t nap; // next allocation "pointer" (offset from base of this) |
| 33 |
struct para_pages * next; |
| 34 |
}; |
| 35 |
|
| 36 |
struct para_pool_private { |
| 37 |
struct para_pages * curr_pages; |
| 38 |
}; |
| 39 |
|
| 40 |
typedef struct para_pool_private para_pool_t; |
| 41 |
|
| 42 |
static inline |
| 43 |
void para_pool_init(para_pool_t * pp) |
| 44 |
{ |
| 45 |
pp->curr_pages = 0; |
| 46 |
} |
| 47 |
|
| 48 |
void para_pool_fini(para_pool_t *); // deallocates everything allocated as well |
| 49 |
|
| 50 |
/* |
| 51 |
static inline |
| 52 |
bool para_pool_more(para_pool_t * pp) |
| 53 |
{ |
| 54 |
size_t sz = NUM_PAGES * get_pagesize(); |
| 55 |
struct para_pages * np = malloc(sz); |
| 56 |
assert(sizeof *np < sz); |
| 57 |
if (np == 0) return false; |
| 58 |
np->next = pp->curr_pages; |
| 59 |
pp->curr_pages = np; |
| 60 |
np->nap = align(sizeof *np, MAX_ALIGN, true); |
| 61 |
np->max = sz; |
| 62 |
return true; |
| 63 |
} |
| 64 |
*/ |
| 65 |
|
| 66 |
// calls para_init |
| 67 |
static inline |
| 68 |
para_t * new_para(para_pool_t * ppo __attribute__((unused)), para_parser_t * ppa) |
| 69 |
{ |
| 70 |
para_t * rv = 0; |
| 71 |
/* |
| 72 |
struct para_pages * cp = ppo->curr_pages; |
| 73 |
if (cp == 0 || cp->nap + sizeof *rv >= cp->max) { |
| 74 |
bool success = para_pool_more(ppo); |
| 75 |
if (!success) return 0; |
| 76 |
cp = ppo->curr_pages; |
| 77 |
} |
| 78 |
assert(cp != 0); |
| 79 |
assert(cp->nap + sizeof *rv < cp->max); |
| 80 |
debug("cp->nap = %zi", cp->nap); |
| 81 |
rv = (para_t *) cp + cp->nap; |
| 82 |
assert((intptr_t)rv % __alignof__(*rv) == 0); |
| 83 |
cp->nap = align(cp->nap + sizeof *rv, MAX_ALIGN, true); |
| 84 |
*/ |
| 85 |
rv = malloc(sizeof *rv); |
| 86 |
if (rv == 0) return 0; |
| 87 |
para_init(ppa, rv); |
| 88 |
return rv; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
#endif /* PARA_POOL_H */ |