| 1 | /* |
| 2 | * Copyright 2011 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: |
| 20 | * Daniel Kopecek <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <stddef.h> |
| 28 | #include <sexp.h> |
| 29 | |
| 30 | #include "../SEAP/generic/rbt/rbt.h" |
| 31 | |
| 32 | #include "rcache.h" |
| 33 | |
| 34 | probe_rcache_t *probe_rcache_new(void) |
| 35 | { |
| 36 | probe_rcache_t *cache; |
| 37 | |
| 38 | cache = malloc(sizeof(probe_rcache_t)); |
| 39 | cache->tree = rbt_str_new(); |
| 40 | |
| 41 | return (cache); |
| 42 | } |
| 43 | |
| 44 | static void probe_rcache_free_node(struct rbt_str_node *n) |
| 45 | { |
| 46 | free(n->key); |
| 47 | SEXP_free(n->data); |
| 48 | } |
| 49 | |
| 50 | void probe_rcache_free(probe_rcache_t *cache) |
| 51 | { |
| 52 | rbt_str_free_cb(cache->tree, &probe_rcache_free_node); |
| 53 | free(cache); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | int probe_rcache_sexp_add(probe_rcache_t *cache, const SEXP_t *id, SEXP_t *item) |
| 58 | { |
| 59 | SEXP_t *r; |
| 60 | char *k; |
| 61 | |
| 62 | if (cache == NULL || id == NULL || item == NULL) { |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | k = SEXP_string_cstr(id); |
| 67 | r = SEXP_ref(item); |
| 68 | |
| 69 | if (rbt_str_add(cache->tree, k, (void *)r) != 0) { |
| 70 | SEXP_free(r); |
| 71 | free(k); |
| 72 | return (-1); |
| 73 | } |
| 74 | |
| 75 | return (0); |
| 76 | } |
| 77 | |
| 78 | int probe_rcache_cstr_add(probe_rcache_t *cache, const char *id, SEXP_t * item) |
| 79 | { |
| 80 | return (-1); |
| 81 | } |
| 82 | |
| 83 | int probe_rcache_sexp_del(probe_rcache_t *cache, const SEXP_t * id) |
| 84 | { |
| 85 | return (-1); |
| 86 | } |
| 87 | |
| 88 | int probe_rcache_cstr_del(probe_rcache_t *cache, const char *id) |
| 89 | { |
| 90 | return (-1); |
| 91 | } |
| 92 | |
| 93 | SEXP_t *probe_rcache_sexp_get(probe_rcache_t *cache, const SEXP_t * id) |
| 94 | { |
| 95 | char b[128], *k = b; |
| 96 | SEXP_t *r = NULL; |
| 97 | |
| 98 | if (SEXP_string_cstr_r(id, k, sizeof b) == ((size_t)-1)) |
| 99 | k = SEXP_string_cstr(id); |
| 100 | |
| 101 | if (k == NULL) |
| 102 | return(NULL); |
| 103 | |
| 104 | rbt_str_get(cache->tree, k, (void *)&r); |
| 105 | |
| 106 | if (k != b) |
| 107 | free(k); |
| 108 | |
| 109 | return (r != NULL ? SEXP_ref(r) : NULL); |
| 110 | } |
| 111 | |
| 112 | SEXP_t *probe_rcache_cstr_get(probe_rcache_t *cache, const char *k) |
| 113 | { |
| 114 | SEXP_t *r = NULL; |
| 115 | |
| 116 | rbt_str_get(cache->tree, k, (void *)&r); |
| 117 | |
| 118 | return (r != NULL ? SEXP_ref(r) : NULL); |
| 119 | } |