« third_party/modest/source/mycore/utils/mcobject_async.c
↑ Summary
third_party/modest/source/mycore/utils/mctree.c »
| 1 | /* | ||||||||||
| 2 | Copyright (C) 2015-2017 Alexander Borisov | ||||||||||
| 3 | |||||||||||
| 4 | This library is free software; you can redistribute it and/or | ||||||||||
| 5 | modify it under the terms of the GNU Lesser General Public | ||||||||||
| 6 | License as published by the Free Software Foundation; either | ||||||||||
| 7 | version 2.1 of the License, or (at your option) any later version. | ||||||||||
| 8 | |||||||||||
| 9 | This library is distributed in the hope that it will be useful, | ||||||||||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||||||||
| 12 | Lesser General Public License for more details. | ||||||||||
| 13 | |||||||||||
| 14 | You should have received a copy of the GNU Lesser General Public | ||||||||||
| 15 | License along with this library; if not, write to the Free Software | ||||||||||
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||||||||||
| 17 | |||||||||||
| 18 | Author: lex.borisov@gmail.com (Alexander Borisov) | ||||||||||
| 19 | */ | ||||||||||
| 20 | |||||||||||
| 21 | #include "mycore/utils/mcsimple.h" | ||||||||||
| 22 | |||||||||||
| 23 | 0 | mcsimple_t * mcsimple_create(void) | |||||||||
| 24 | { | ||||||||||
| 25 | 0 | return mycore_calloc(1, sizeof(mcsimple_t)); | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | 0 | void mcsimple_init(mcsimple_t *mcsimple, size_t pos_size, size_t list_size, size_t struct_size) | |||||||||
| 29 | { | ||||||||||
| 30 | 0 | mcsimple->struct_size = struct_size; | |||||||||
| 31 | |||||||||||
| 32 | 0 | mcsimple->list_pos_length_used = 0; | |||||||||
| 33 | 0 | mcsimple->list_pos_length = 0; | |||||||||
| 34 | 0 | mcsimple->list_pos_size = pos_size; | |||||||||
| 35 | 0 | mcsimple->list = (uint8_t**)mycore_calloc(pos_size, sizeof(uint8_t*)); | |||||||||
| 36 | |||||||||||
| 37 | 0 | ▶ | if(mcsimple->list == NULL) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 0 | return; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 0 | mcsimple->list_size = list_size * struct_size; | |||||||||
| 42 | |||||||||||
| 43 | 0 | ▶ | if((mcsimple_init_list_entries(mcsimple, mcsimple->list_pos_length) == NULL)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 0 | return; | |||||||||
| 45 | } | ||||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 0 | void mcsimple_clean(mcsimple_t *mcsimple) | |||||||||
| 49 | { | ||||||||||
| 50 | 0 | mcsimple->list_length = 0; | |||||||||
| 51 | 0 | mcsimple->list_pos_length = 0; | |||||||||
| 52 | 0 | } | |||||||||
| 53 | |||||||||||
| 54 | 0 | mcsimple_t * mcsimple_destroy(mcsimple_t *mcsimple, bool destroy_self) | |||||||||
| 55 | { | ||||||||||
| 56 | 0 | ▶ | if(mcsimple == NULL) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 0 | return NULL; | |||||||||
| 58 | |||||||||||
| 59 | 0 | ▶ | if(mcsimple->list) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 0 | ▶ | for(size_t i = 0; i < mcsimple->list_pos_length_used; i++) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 0 | ▶ | if(mcsimple->list[i]) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 0 | mycore_free(mcsimple->list[i]); | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 0 | mycore_free(mcsimple->list); | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 0 | ▶ | if(destroy_self) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 0 | mycore_free(mcsimple); | |||||||||
| 70 | 0 | return NULL; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 0 | return mcsimple; | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 0 | uint8_t * mcsimple_init_list_entries(mcsimple_t *mcsimple, size_t pos) | |||||||||
| 77 | { | ||||||||||
| 78 | 0 | ▶ | if(mcsimple->list_pos_length >= mcsimple->list_pos_size) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | { | ||||||||||
| 80 | 0 | size_t new_size = mcsimple->list_pos_size + 128; | |||||||||
| 81 | 0 | uint8_t **list = (uint8_t**)mycore_realloc(mcsimple->list, mcsimple->list_pos_size * sizeof(uint8_t*)); | |||||||||
| 82 | |||||||||||
| 83 | 0 | ▶ | if(list) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 0 | mcsimple->list = list; | |||||||||
| 85 | 0 | memset(&mcsimple->list[pos], 0, (new_size - mcsimple->list_pos_size) * sizeof(uint8_t*)); | |||||||||
| 86 | |||||||||||
| 87 | 0 | mcsimple->list_pos_size = new_size; | |||||||||
| 88 | } | ||||||||||
| 89 | else | ||||||||||
| 90 | 0 | return NULL; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 0 | mcsimple->list_length = 0; | |||||||||
| 94 | |||||||||||
| 95 | 0 | ▶ | if(mcsimple->list[pos] == NULL) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 0 | mcsimple->list_pos_length_used++; | |||||||||
| 97 | 0 | mcsimple->list[pos] = (uint8_t*)mycore_malloc(mcsimple->list_size * sizeof(uint8_t)); | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 0 | return mcsimple->list[pos]; | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 0 | void * mcsimple_malloc(mcsimple_t *mcsimple) | |||||||||
| 104 | { | ||||||||||
| 105 | 0 | ▶ | if(mcsimple->list_length >= mcsimple->list_size) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | { | ||||||||||
| 107 | 0 | mcsimple->list_pos_length++; | |||||||||
| 108 | 0 | ▶ | if((mcsimple_init_list_entries(mcsimple, mcsimple->list_pos_length) == NULL)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | 0 | return NULL; | |||||||||
| 110 | } | ||||||||||
| 111 | } | ||||||||||
| 112 | |||||||||||
| 113 | 0 | size_t current = mcsimple->list_length; | |||||||||
| 114 | 0 | mcsimple->list_length += mcsimple->struct_size; | |||||||||
| 115 | |||||||||||
| 116 | 0 | return &mcsimple->list[mcsimple->list_pos_length][current]; | |||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | 0 | void * mcsimple_get_by_absolute_position(mcsimple_t *mcsimple, size_t pos) | |||||||||
| 120 | { | ||||||||||
| 121 | 0 | pos *= mcsimple->struct_size; | |||||||||
| 122 | |||||||||||
| 123 | 0 | size_t list_index = pos / mcsimple->list_size; | |||||||||
| 124 | 0 | ▶ | if(list_index >= mcsimple->list_pos_length_used) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 125 | 0 | return NULL; | |||||||||
| 126 | |||||||||||
| 127 | 0 | return &mcsimple->list[list_index][ (pos % mcsimple->list_size) ]; | |||||||||
| 128 | } | ||||||||||
| 129 | |||||||||||
| 130 | |||||||||||