| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2008 The Native Client Authors. All rights reserved. | 2 * Copyright 2008 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can | 3 * Use of this source code is governed by a BSD-style license that can |
| 4 * be found in the LICENSE file. | 4 * be found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 /* | 7 #include "native_client/src/trusted/generic_container/container_hash_table.h" |
| 8 * NaCl Generic containers. | 8 |
| 9 */ | |
| 10 #include "native_client/src/include/portability.h" | 9 #include "native_client/src/include/portability.h" |
| 11 #include <stdio.h> | |
| 12 #include <stdlib.h> | |
| 13 | |
| 14 #include "native_client/src/trusted/service_runtime/generic_container/container.
h" | |
| 15 | |
| 16 static struct NaClContainerVtbl const kNaClContainerListVtbl = { | |
| 17 NaClContainerListInsert, | |
| 18 NaClContainerListFind, | |
| 19 NaClContainerListDtor, | |
| 20 sizeof(struct NaClContainerListIter), | |
| 21 NaClContainerListIterCtor, | |
| 22 }; | |
| 23 | |
| 24 | |
| 25 /* | |
| 26 * functor_obj is the comparison functor, and is the first argument to | |
| 27 * cmp_fn as the "self" (or more commonly "this") argument. arguably | |
| 28 * we should create an explicit object with a vtable containing the | |
| 29 * single comparison function pointer, but this is so simple that that | |
| 30 * seems overkill. | |
| 31 */ | |
| 32 int NaClContainerListCtor(struct NaClContainerList *self, | |
| 33 struct NaClCmpFunctor *cmp_functor) { | |
| 34 self->cmp_functor = cmp_functor; | |
| 35 self->head = 0; | |
| 36 | |
| 37 self->base.vtbl = &kNaClContainerListVtbl; | |
| 38 return 1; | |
| 39 } | |
| 40 | |
| 41 | |
| 42 int NaClContainerListIterAtEnd(struct NaClContainerIter *vself) { | |
| 43 struct NaClContainerListIter *self = (struct NaClContainerListIter *) vself; | |
| 44 | |
| 45 return *self->cur == 0; | |
| 46 } | |
| 47 | |
| 48 | |
| 49 void *NaClContainerListIterStar(struct NaClContainerIter *vself) { | |
| 50 struct NaClContainerListIter *self = (struct NaClContainerListIter *) vself; | |
| 51 | |
| 52 return (*self->cur)->datum; | |
| 53 } | |
| 54 | |
| 55 | |
| 56 void NaClContainerListIterIncr(struct NaClContainerIter *vself) { | |
| 57 struct NaClContainerListIter *self = (struct NaClContainerListIter *) vself; | |
| 58 | |
| 59 self->cur = &(*self->cur)->next; | |
| 60 } | |
| 61 | |
| 62 | |
| 63 void NaClContainerListIterErase(struct NaClContainerIter *vself) { | |
| 64 struct NaClContainerListIter *self = (struct NaClContainerListIter *) vself; | |
| 65 struct NaClItemList *doomed = *self->cur; | |
| 66 struct NaClItemList *next = (*self->cur)->next; | |
| 67 | |
| 68 *self->cur = next; | |
| 69 free(doomed->datum); | |
| 70 free(doomed); | |
| 71 } | |
| 72 | |
| 73 | |
| 74 void *NaClContainerListIterExtract(struct NaClContainerIter *vself) { | |
| 75 struct NaClContainerListIter *self = (struct NaClContainerListIter *) vself; | |
| 76 struct NaClItemList *doomed = *self->cur; | |
| 77 struct NaClItemList *next = (*self->cur)->next; | |
| 78 void *datum; | |
| 79 | |
| 80 *self->cur = next; | |
| 81 datum = doomed->datum; | |
| 82 free(doomed); | |
| 83 return datum; | |
| 84 } | |
| 85 | |
| 86 static struct NaClContainerIterVtbl const kNaClContainerListIterVtbl = { | |
| 87 NaClContainerListIterAtEnd, | |
| 88 NaClContainerListIterStar, | |
| 89 NaClContainerListIterIncr, | |
| 90 NaClContainerListIterErase, | |
| 91 NaClContainerListIterExtract, | |
| 92 }; | |
| 93 | |
| 94 | |
| 95 int NaClContainerListInsert(struct NaClContainer *vself, | |
| 96 void *obj) { | |
| 97 struct NaClContainerList *self = (struct NaClContainerList *) vself; | |
| 98 struct NaClItemList *new_entry = malloc(sizeof *new_entry); | |
| 99 struct NaClItemList **ipp; | |
| 100 struct NaClItemList *ip; | |
| 101 | |
| 102 if (!new_entry) | |
| 103 return 0; | |
| 104 | |
| 105 new_entry->datum = obj; | |
| 106 | |
| 107 for (ipp = &self->head; 0 != (ip = *ipp); ipp = &ip->next) { | |
| 108 if ((*self->cmp_functor->vtbl->OrderCmp)(self->cmp_functor, | |
| 109 obj, ip->datum) >= 0) { | |
| 110 break; | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 new_entry->next = ip; | |
| 115 *ipp = new_entry; | |
| 116 return 1; | |
| 117 } | |
| 118 | |
| 119 | |
| 120 struct NaClContainerIter *NaClContainerListFind( | |
| 121 struct NaClContainer *vself, | |
| 122 void *key, | |
| 123 struct NaClContainerIter *vout) { | |
| 124 struct NaClContainerList *self = (struct NaClContainerList *) vself; | |
| 125 struct NaClItemList **ipp; | |
| 126 struct NaClItemList *ip; | |
| 127 struct NaClContainerListIter *out = (struct NaClContainerListIter *) vout; | |
| 128 | |
| 129 for (ipp = &self->head; (ip = *ipp) != 0; ipp = &ip->next) { | |
| 130 if ((*self->cmp_functor->vtbl->OrderCmp)(self->cmp_functor, | |
| 131 key, ip->datum) == 0) { | |
| 132 break; | |
| 133 } | |
| 134 } | |
| 135 out->cur = ipp; | |
| 136 | |
| 137 vout->vtbl = &kNaClContainerListIterVtbl; | |
| 138 return vout; | |
| 139 } | |
| 140 | |
| 141 | |
| 142 void NaClContainerListDtor(struct NaClContainer *vself) { | |
| 143 struct NaClContainerList *self = (struct NaClContainerList *) vself; | |
| 144 struct NaClItemList *cur, *next; | |
| 145 | |
| 146 for (cur = self->head; cur; cur = next) { | |
| 147 next = cur->next; | |
| 148 free(cur->datum); | |
| 149 free(cur); | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 | |
| 154 int NaClContainerListIterCtor(struct NaClContainer *vself, | |
| 155 struct NaClContainerIter *viter) { | |
| 156 struct NaClContainerList *self = (struct NaClContainerList *) vself; | |
| 157 struct NaClContainerListIter *iter = (struct NaClContainerListIter *) viter; | |
| 158 | |
| 159 iter->cur = &self->head; | |
| 160 | |
| 161 viter->vtbl = &kNaClContainerListIterVtbl; | |
| 162 return 1; | |
| 163 } | |
| 164 | |
| 165 | 10 |
| 166 static struct NaClContainerVtbl const kNaClContainerHashTblVtbl = { | 11 static struct NaClContainerVtbl const kNaClContainerHashTblVtbl = { |
| 167 NaClContainerHashTblInsert, | 12 NaClContainerHashTblInsert, |
| 168 NaClContainerHashTblFind, | 13 NaClContainerHashTblFind, |
| 169 NaClContainerHashTblDtor, | 14 NaClContainerHashTblDtor, |
| 170 sizeof(struct NaClContainerHashTblIter), | 15 sizeof(struct NaClContainerHashTblIter), |
| 171 NaClContainerHashTblIterCtor, | 16 NaClContainerHashTblIterCtor, |
| 172 }; | 17 }; |
| 173 | 18 |
| 174 #define HASH_TABLE_LOAD_FACTOR 2 | 19 #define HASH_TABLE_LOAD_FACTOR 2 |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 462 = (struct NaClContainerHashTblIter *) viter; | 307 = (struct NaClContainerHashTblIter *) viter; |
| 463 | 308 |
| 464 viter->vtbl = &kNaClContainerHashTblIterVtbl; | 309 viter->vtbl = &kNaClContainerHashTblIterVtbl; |
| 465 iter->htbl = self; | 310 iter->htbl = self; |
| 466 iter->idx = 0; | 311 iter->idx = 0; |
| 467 if (0 == (self->bucket[0].flags & NACL_CHTE_USED)) { | 312 if (0 == (self->bucket[0].flags & NACL_CHTE_USED)) { |
| 468 NaClContainerHashTblIterIncr(viter); | 313 NaClContainerHashTblIterIncr(viter); |
| 469 } | 314 } |
| 470 return 1; | 315 return 1; |
| 471 } | 316 } |
| OLD | NEW |