]> arthur.barton.de Git - netatalk.git/blob - libatalk/talloc/dalloc.c
2390d108ebc9c438d6ece5ecace1aa1b8bc61875
[netatalk.git] / libatalk / talloc / dalloc.c
1 /*
2   Copyright (c) 2012 Frank Lahm <franklahm@gmail.com>
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
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif /* HAVE_CONFIG_H */
18
19 #include <string.h>
20 #include <strings.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <stdbool.h>
25 #include <inttypes.h>
26
27 #include <atalk/errchk.h>
28 #include <atalk/util.h>
29 #include <atalk/logger.h>
30 #include <atalk/talloc.h>
31 #include <atalk/bstrlib.h>
32 #include <atalk/dalloc.h>
33
34 /* Use dalloc_add() macro, not this function */
35 int dalloc_add_talloc_chunk(DALLOC_CTX *dd, void *talloc_chunk, void *obj, size_t size)
36 {
37     AFP_ASSERT(talloc_chunk);
38
39     dd->dd_talloc_array = talloc_realloc(dd, dd->dd_talloc_array, void *, talloc_array_length(dd->dd_talloc_array) + 1);
40     memcpy(talloc_chunk, obj, size);
41     dd->dd_talloc_array[talloc_array_length(dd->dd_talloc_array) - 1] = talloc_chunk;
42
43     return 0;
44 }
45
46 void *dalloc_get(DALLOC_CTX *d, ...)
47 {
48     EC_INIT;
49     void *p = NULL;
50     va_list args;
51     const char *type;
52     int elem;
53     const char *elemtype;
54
55     va_start(args, d);
56     type = va_arg(args, const char *);
57
58     if (STRCMP(type, !=, "DALLOC_CTX"))
59         EC_FAIL;
60
61     while (STRCMP(type, ==, "DALLOC_CTX")) {
62         elem = va_arg(args, int);
63         AFP_ASSERT(elem < talloc_array_length(d->dd_talloc_array));
64         d = d->dd_talloc_array[elem];
65         type = va_arg(args, const char *);
66     }
67
68     elem = va_arg(args, int);
69     AFP_ASSERT(elem < talloc_array_length(d->dd_talloc_array));
70
71     p = talloc_check_name(d->dd_talloc_array[elem], type);
72
73     va_end(args);
74
75 EC_CLEANUP:
76     if (ret != 0)
77         p = NULL;
78     return p;
79 }