]> arthur.barton.de Git - netatalk.git/blob - libatalk/talloc/dalloc.c
7f4ffd36a1f5d16e307f6cd950fa1a63057834cf
[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 /* Get number of elements, returns 0 if the structure is empty or not initialized */
47 int dalloc_size(DALLOC_CTX *d)
48 {
49     if (!d || !d->dd_talloc_array)
50         return 0;
51     return talloc_array_length(d->dd_talloc_array);
52 }
53
54 void *dalloc_get(const DALLOC_CTX *d, ...)
55 {
56     EC_INIT;
57     void *p = NULL;
58     va_list args;
59     const char *type;
60     int elem;
61     const char *elemtype;
62
63     va_start(args, d);
64     type = va_arg(args, const char *);
65
66     if (STRCMP(type, !=, "DALLOC_CTX"))
67         EC_FAIL;
68
69     while (STRCMP(type, ==, "DALLOC_CTX")) {
70         elem = va_arg(args, int);
71         AFP_ASSERT(elem < talloc_array_length(d->dd_talloc_array));
72         d = d->dd_talloc_array[elem];
73         type = va_arg(args, const char *);
74     }
75
76     elem = va_arg(args, int);
77     AFP_ASSERT(elem < talloc_array_length(d->dd_talloc_array));
78
79     p = talloc_check_name(d->dd_talloc_array[elem], type);
80
81     va_end(args);
82
83 EC_CLEANUP:
84     if (ret != 0)
85         p = NULL;
86     return p;
87 }
88
89 void *dalloc_value_for_key(const DALLOC_CTX *d, ...)
90 {
91     EC_INIT;
92     void *p = NULL;
93     va_list args;
94     const char *type;
95     int elem;
96     const char *elemtype;
97     char *s;
98
99     va_start(args, d);
100     type = va_arg(args, const char *);
101
102     if (STRCMP(type, !=, "DALLOC_CTX"))
103         EC_FAIL;
104
105     while (STRCMP(type, ==, "DALLOC_CTX")) {
106         elem = va_arg(args, int);
107         AFP_ASSERT(elem < talloc_array_length(d->dd_talloc_array));
108         d = d->dd_talloc_array[elem];
109         type = va_arg(args, const char *);
110     }
111
112     for (elem = 0; elem + 1 < talloc_array_length(d->dd_talloc_array); elem += 2) {
113         memcpy(&s, d->dd_talloc_array[elem], sizeof(char *));
114         if (STRCMP(s, ==, type)) {
115             p = d->dd_talloc_array[elem + 1];
116             break;
117         }            
118     }
119     va_end(args);
120
121 EC_CLEANUP:
122     if (ret != 0)
123         p = NULL;
124     return p;
125 }