Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 1 | /** |
| 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 Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame^] | 25 | #include <stdint.h> |
| 26 | |
| 27 | /* |
| 28 | * structure definition from context.h |
| 29 | */ |
| 30 | struct 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 | */ |
| 42 | struct 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 | */ |
| 52 | struct 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 | */ |
| 63 | void lydict_init(struct dict_table *dict); |
| 64 | |
| 65 | /** |
| 66 | * @brief Cleanup the dictionary content |
| 67 | * |
| 68 | * @param[in] dict Dictionary table to cleanup |
| 69 | */ |
| 70 | void lydict_clean(struct dict_table *dict); |
| 71 | |
Radek Krejci | 4ea0838 | 2015-04-21 09:41:40 +0200 | [diff] [blame] | 72 | /** |
| 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 Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame^] | 77 | * @param[in] ctx libyang context handler |
Radek Krejci | 4ea0838 | 2015-04-21 09:41:40 +0200 | [diff] [blame] | 78 | * @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 Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame^] | 85 | char *lydict_insert(struct ly_ctx *ctx, const char *value, size_t len); |
Radek Krejci | 4ea0838 | 2015-04-21 09:41:40 +0200 | [diff] [blame] | 86 | |
| 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 Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame^] | 93 | * @param[in] ctx libyang context handler |
Radek Krejci | 4ea0838 | 2015-04-21 09:41:40 +0200 | [diff] [blame] | 94 | * @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 Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame^] | 101 | char *lydict_insert_zc(struct ly_ctx *ctx, char *value); |
Radek Krejci | 4ea0838 | 2015-04-21 09:41:40 +0200 | [diff] [blame] | 102 | |
| 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 Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame^] | 108 | * @param[in] ctx libyang context handler |
| 109 | * @param[in] value String to be freed. Note, that not only the string itself |
Radek Krejci | 4ea0838 | 2015-04-21 09:41:40 +0200 | [diff] [blame] | 110 | * must match the stored value, but also the address is being compared and the |
| 111 | * counter is decremented only if it matches. |
| 112 | */ |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame^] | 113 | void lydict_remove(struct ly_ctx *ctx, const char *value); |
Radek Krejci | ee62758 | 2015-04-20 17:39:59 +0200 | [diff] [blame] | 114 | |
| 115 | #endif /* LY_DICT_H_ */ |