]> arthur.barton.de Git - netatalk.git/blob - libatalk/talloc/dalloc.c
Don't use fixed tag when marshalling the CNID structure
[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_copy() macro, not this function */
35 int dalloc_add_talloc_chunk(DALLOC_CTX *dd, void *talloc_chunk, void *obj, size_t size)
36 {
37     if (talloc_chunk) {
38         /* Called from dalloc_add_copy() macro */
39         dd->dd_talloc_array = talloc_realloc(dd,
40                                              dd->dd_talloc_array,
41                                              void *,
42                                              talloc_array_length(dd->dd_talloc_array) + 1);
43         memcpy(talloc_chunk, obj, size);
44         dd->dd_talloc_array[talloc_array_length(dd->dd_talloc_array) - 1] = talloc_chunk;
45     } else {
46         /* Called from dalloc_add() macro */
47         dd->dd_talloc_array = talloc_realloc(dd,
48                                              dd->dd_talloc_array,
49                                              void *,
50                                              talloc_array_length(dd->dd_talloc_array) + 1);
51         dd->dd_talloc_array[talloc_array_length(dd->dd_talloc_array) - 1] = obj;
52
53     }
54     return 0;
55 }
56
57 /* Get number of elements, returns 0 if the structure is empty or not initialized */
58 int dalloc_size(DALLOC_CTX *d)
59 {
60     if (!d || !d->dd_talloc_array)
61         return 0;
62     return talloc_array_length(d->dd_talloc_array);
63 }
64
65 void *dalloc_get(const DALLOC_CTX *d, ...)
66 {
67     EC_INIT;
68     void *p = NULL;
69     va_list args;
70     const char *type;
71     int elem;
72     const char *elemtype;
73
74     va_start(args, d);
75     type = va_arg(args, const char *);
76
77     if (STRCMP(type, !=, "DALLOC_CTX"))
78         EC_FAIL;
79
80     while (STRCMP(type, ==, "DALLOC_CTX")) {
81         elem = va_arg(args, int);
82         AFP_ASSERT(elem < talloc_array_length(d->dd_talloc_array));
83         d = d->dd_talloc_array[elem];
84         type = va_arg(args, const char *);
85     }
86
87     elem = va_arg(args, int);
88     AFP_ASSERT(elem < talloc_array_length(d->dd_talloc_array));
89
90     p = talloc_check_name(d->dd_talloc_array[elem], type);
91
92     va_end(args);
93
94 EC_CLEANUP:
95     if (ret != 0)
96         p = NULL;
97     return p;
98 }
99
100 void *dalloc_value_for_key(const DALLOC_CTX *d, ...)
101 {
102     EC_INIT;
103     void *p = NULL;
104     va_list args;
105     const char *type;
106     int elem;
107     const char *elemtype;
108     char *s;
109
110     va_start(args, d);
111     type = va_arg(args, const char *);
112
113     if (STRCMP(type, !=, "DALLOC_CTX"))
114         EC_FAIL;
115
116     while (STRCMP(type, ==, "DALLOC_CTX")) {
117         elem = va_arg(args, int);
118         AFP_ASSERT(elem < talloc_array_length(d->dd_talloc_array));
119         d = d->dd_talloc_array[elem];
120         type = va_arg(args, const char *);
121     }
122
123     for (elem = 0; elem + 1 < talloc_array_length(d->dd_talloc_array); elem += 2) {
124         if (STRCMP(talloc_get_name(d->dd_talloc_array[elem]), !=, "char *")) {
125             LOG(log_error, logtype_default, "dalloc_value_for_key: key not a string: %s",
126                 talloc_get_name(d->dd_talloc_array[elem]));
127             EC_FAIL;
128         }
129         if (STRCMP((char *)d->dd_talloc_array[elem], ==, type)) {
130             p = d->dd_talloc_array[elem + 1];
131             break;
132         }            
133     }
134     va_end(args);
135
136 EC_CLEANUP:
137     if (ret != 0)
138         p = NULL;
139     return p;
140 }
141
142 char *dalloc_strdup(const void *ctx, const char *string)
143 {
144     EC_INIT;
145     char *p;
146
147     EC_NULL( p = talloc_strdup(ctx, string) );
148     talloc_set_name(p, "char *");
149
150 EC_CLEANUP:
151     if (ret != 0) {
152         if (p)
153             talloc_free(p);
154         p = NULL;
155     }
156     return p;
157 }
158
159 char *dalloc_strndup(const void *ctx, const char *string, size_t n)
160 {
161     EC_INIT;
162     char *p;
163
164     EC_NULL( p = talloc_strndup(ctx, string, n) );
165     talloc_set_name(p, "char *");
166
167 EC_CLEANUP:
168     if (ret != 0) {
169         if (p)
170             talloc_free(p);
171         p = NULL;
172     }
173     return p;
174 }