blob: 52bbd7a480632a34370b6f4eecefdb2f644c903b [file] [log] [blame]
Radek Krejciee627582015-04-20 17:39:59 +02001/**
2 * @file dict.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief libyang dictionary
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of the Company nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 */
21
22#ifndef LY_DICT_H_
23#define LY_DICT_H_
24
Radek Krejcida04f4a2015-05-21 12:54:09 +020025#include <stdint.h>
26
27/*
28 * structure definition from context.h
29 */
30struct ly_ctx;
31
32/**
33 * size of the dictionary for each context
34 */
35#define DICT_SIZE 1024
36
37/**
38 * record of the dictionary
39 * TODO: save the next pointer by different collision strategy, will need to
40 * make dictionary size dynamic
41 */
42struct dict_rec {
43 char *value;
44 uint32_t refcount;
45 struct dict_rec *next;
46};
47
48/**
49 * dictionary to store repeating strings
50 * TODO: make it variable size
51 */
52struct dict_table {
53 struct dict_rec recs[DICT_SIZE];
54 int hash_mask;
55 uint32_t used;
56};
57
58/**
59 * @brief Initiate content (non-zero values) of the dictionary
60 *
61 * @param[in] dict Dictionary table to initiate
62 */
63void lydict_init(struct dict_table *dict);
64
65/**
66 * @brief Cleanup the dictionary content
67 *
68 * @param[in] dict Dictionary table to cleanup
69 */
70void lydict_clean(struct dict_table *dict);
71
Radek Krejci4ea08382015-04-21 09:41:40 +020072/**
73 * @brief Insert string into dictionary. If the string is already present,
74 * only a reference counter is incremented and no memory allocation is
75 * performed.
76 *
Radek Krejcida04f4a2015-05-21 12:54:09 +020077 * @param[in] ctx libyang context handler
Radek Krejci4ea08382015-04-21 09:41:40 +020078 * @param[in] value String to be stored in the dictionary.
79 * @param[in] len Number of bytes to store. The value is not required to be
80 * NULL terminated string, the len parameter says number of bytes stored in
81 * dictionary. The specified number of bytes is duplicated and terminating NULL
82 * byte is added automatically.
83 * @return pointer to the string stored in the dictionary
84 */
Radek Krejcida04f4a2015-05-21 12:54:09 +020085char *lydict_insert(struct ly_ctx *ctx, const char *value, size_t len);
Radek Krejci4ea08382015-04-21 09:41:40 +020086
87/**
88 * @brief Insert string into dictionary - zerocopy version. If the string is
89 * already present, only a reference counter is incremented and no memory
90 * allocation is performed. This insert function variant avoids duplication of
91 * specified value - it is inserted into the dictionary directly.
92 *
Radek Krejcida04f4a2015-05-21 12:54:09 +020093 * @param[in] ctx libyang context handler
Radek Krejci4ea08382015-04-21 09:41:40 +020094 * @param[in] value NULL-terminated string to be stored in the dictionary. If
95 * the string is not present in dictionary, the pointer is directly used by the
96 * dictionary. Otherwise, the reference counter is incremented and the value is
97 * freed. So, after calling the function, caller is supposed to not use the
98 * value address anymore.
99 * @return pointer to the string stored in the dictionary
100 */
Radek Krejcida04f4a2015-05-21 12:54:09 +0200101char *lydict_insert_zc(struct ly_ctx *ctx, char *value);
Radek Krejci4ea08382015-04-21 09:41:40 +0200102
103
104/**
105 * @brief Remove specified string from the dictionary. It decrement reference
106 * counter for the string and if it is zero, the string itself is freed.
107 *
Radek Krejcida04f4a2015-05-21 12:54:09 +0200108 * @param[in] ctx libyang context handler
109 * @param[in] value String to be freed. Note, that not only the string itself
Radek Krejci4ea08382015-04-21 09:41:40 +0200110 * must match the stored value, but also the address is being compared and the
111 * counter is decremented only if it matches.
112 */
Radek Krejcida04f4a2015-05-21 12:54:09 +0200113void lydict_remove(struct ly_ctx *ctx, const char *value);
Radek Krejciee627582015-04-20 17:39:59 +0200114
115#endif /* LY_DICT_H_ */