taskrambler  0.1.9
Web server and task management solution.
hash/value.c
Go to the documentation of this file.
1 /**
2  * \file
3  *
4  * \author Georg Hopp
5  *
6  * \copyright
7  * Copyright © 2012 Georg Hopp
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27 
28 #include "class.h"
29 #include "utils/hash.h"
30 #include "utils/memory.h"
31 #include "commons.h"
32 
33 #include "hash/value.h"
35 
36 static
37 int
38 hashValueCtor(void * _this, va_list * params)
39 {
40  HashValue this = _this;
41  char * key = va_arg(* params, char*);
42  void * value;
43 
44  this->nkey = va_arg(* params, size_t);
45  value = va_arg(* params, void*);
46  this->nvalue = va_arg(* params, size_t);
47 
48  this->key = memMalloc(this->nkey + 1);
49  this->key[this->nkey] = 0;
50  memcpy(this->key, key, this->nkey);
51 
52  this->hash = sdbm((unsigned char *)this->key, this->nkey);
53 
54  if (NULL != value) {
55  this->value = memMalloc(this->nvalue + 1);
56  ((char*)this->value)[this->nvalue] = 0;
57  memcpy(this->value, value, this->nvalue);
58  }
59 
60  return 0;
61 }
62 
63 static
64 void
65 hashValueDtor(void * _this)
66 {
67  HashValue this = _this;
68 
69  MEM_FREE(this->key);
70  MEM_FREE(this->value);
71 }
72 
73 static
74 unsigned long
75 hashValueGetHash(void * _this)
76 {
77  HashValue this = _this;
78 
79  return this->hash;
80 }
81 
82 static
83 void
84 hashValueHandleDouble(void * _this, void * _double)
85 {
86  HashValue this = _this;
87  HashValue doub = _double;
88  void * tmp_value;
89  size_t tmp_nvalue;
90 
91  /**
92  * here we swap the internal data of both objects,
93  * effectively overwriting the old entry. We need not
94  * to free anything here as _double will be deleted
95  * afterwards anyway (\see hash/add.c).
96  */
97  tmp_value = this->value;
98  this->value = doub->value;
99  doub->value = tmp_value;
100 
101  tmp_nvalue = this->nvalue;
102  this->nvalue = doub->nvalue;
103  doub->nvalue = tmp_nvalue;
104 }
105 
106 INIT_IFACE(Class, hashValueCtor, hashValueDtor, NULL);
108 CREATE_CLASS(HashValue, NULL, IFACE(Class), IFACE(Hashable));
109 
110 // vim: set ts=4 sw=4:
#define MEM_FREE(seg)
Definition: memory.h:28
static void hashValueDtor(void *_this)
Definition: hash/value.c:65
static void hashValueHandleDouble(void *_this, void *_double)
Definition: hash/value.c:84
unsigned long sdbm(const unsigned char *, size_t)
Definition: utils/hash.c:45
void * memMalloc(size_t)
Definition: memory.c:783
#define IFACE(name)
Definition: interface.h:34
static int hashValueCtor(void *_this, va_list *params)
Definition: hash/value.c:38
CREATE_CLASS(HashValue, NULL, IFACE(Class), IFACE(Hashable))
static unsigned long hashValueGetHash(void *_this)
Definition: hash/value.c:75
INIT_IFACE(Class, hashValueCtor, hashValueDtor, NULL)