Debian Codegraph @

libmowgli-2/src/libmowgli/core/allocation_policy.c

raw
1 /*
2 * libmowgli: A collection of useful routines for programming.
3 * allocation_policy.h: Allocation policy management.
4 *
5 * Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk>
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice is present in all copies.
10 *
11 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
12 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
13 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
14 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
15 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
20 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21 * POSSIBILITY OF SUCH DAMAGE.
22 */
23
24 #include "mowgli.h"
25 #include "core/bootstrap_internal.h"
26
27 static mowgli_object_class_t klass;
28 static mowgli_patricia_t *mowgli_allocation_policy_dict = NULL;
29
30 static void
31 _allocation_policy_key_canon(char *str)
32 { }
33
34 void
35 mowgli_allocation_policy_bootstrap(void)
36 {
37 mowgli_allocation_policy_dict = mowgli_patricia_create(_allocation_policy_key_canon);
38
39 mowgli_object_class_init(&klass, "mowgli.allocation_policy", NULL, FALSE);
40 }
41
42 mowgli_allocation_policy_t *
43 mowgli_allocation_policy_create(const char *name, mowgli_allocation_func_t allocator, mowgli_deallocation_func_t deallocator)
44 {
45 mowgli_allocation_policy_t *policy;
46
47 if (mowgli_allocation_policy_dict == NULL)
48 mowgli_allocation_policy_dict = mowgli_patricia_create(_allocation_policy_key_canon);
49
50 if ((policy = mowgli_patricia_retrieve(mowgli_allocation_policy_dict, name)))
51 return policy;
52
53 policy = mowgli_alloc(sizeof *policy);
54 mowgli_object_init_from_class(mowgli_object(policy), name, &klass);
55
56 policy->allocate = allocator;
57 policy->deallocate = deallocator;
58
59 mowgli_patricia_add(mowgli_allocation_policy_dict, name, policy);
60
61 return policy;
62 }
63
64 mowgli_allocation_policy_t *
65 mowgli_allocation_policy_lookup(const char *name)
66 {
67 if (mowgli_allocation_policy_dict == NULL)
68 mowgli_allocation_policy_dict = mowgli_patricia_create(_allocation_policy_key_canon);
69
70 return mowgli_patricia_retrieve(mowgli_allocation_policy_dict, name);
71 }