2 Copyright (c) 2012 Frank Lahm <franklahm@gmail.com>
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.
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.
17 #endif /* HAVE_CONFIG_H */
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>
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)
38 /* Called from dalloc_add_copy() macro */
39 dd->dd_talloc_array = talloc_realloc(dd,
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;
46 /* Called from dalloc_add() macro */
47 dd->dd_talloc_array = talloc_realloc(dd,
50 talloc_array_length(dd->dd_talloc_array) + 1);
51 dd->dd_talloc_array[talloc_array_length(dd->dd_talloc_array) - 1] = obj;
57 /* Get number of elements, returns 0 if the structure is empty or not initialized */
58 int dalloc_size(DALLOC_CTX *d)
60 if (!d || !d->dd_talloc_array)
62 return talloc_array_length(d->dd_talloc_array);
65 void *dalloc_get(const DALLOC_CTX *d, ...)
75 type = va_arg(args, const char *);
77 if (STRCMP(type, !=, "DALLOC_CTX"))
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 *);
87 elem = va_arg(args, int);
88 AFP_ASSERT(elem < talloc_array_length(d->dd_talloc_array));
90 p = talloc_check_name(d->dd_talloc_array[elem], type);
100 void *dalloc_value_for_key(const DALLOC_CTX *d, ...)
107 const char *elemtype;
111 type = va_arg(args, const char *);
113 if (STRCMP(type, !=, "DALLOC_CTX"))
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 *);
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]));
129 if (STRCMP((char *)d->dd_talloc_array[elem], ==, type)) {
130 p = d->dd_talloc_array[elem + 1];
142 char *dalloc_strdup(const void *ctx, const char *string)
147 EC_NULL( p = talloc_strdup(ctx, string) );
148 talloc_set_name(p, "char *");
159 char *dalloc_strndup(const void *ctx, const char *string, size_t n)
164 EC_NULL( p = talloc_strndup(ctx, string, n) );
165 talloc_set_name(p, "char *");