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 be
4 * found in the LICENSE file.
7 #include "native_client/src/trusted/generic_container/container_list.h"
9 #include "native_client/src/include/portability.h"
11 static struct NaClContainerVtbl const kNaClContainerListVtbl = {
12 NaClContainerListInsert,
13 NaClContainerListFind,
14 NaClContainerListDtor,
15 sizeof(struct NaClContainerListIter),
16 NaClContainerListIterCtor,
21 * functor_obj is the comparison functor, and is the first argument to
22 * cmp_fn as the "self" (or more commonly "this") argument. arguably
23 * we should create an explicit object with a vtable containing the
24 * single comparison function pointer, but this is so simple that that
27 int NaClContainerListCtor(struct NaClContainerList *self,
28 struct NaClCmpFunctor *cmp_functor) {
29 self->cmp_functor = cmp_functor;
32 self->base.vtbl = &kNaClContainerListVtbl;
37 int NaClContainerListIterAtEnd(struct NaClContainerIter *vself) {
38 struct NaClContainerListIter *self = (struct NaClContainerListIter *) vself;
40 return *self->cur == 0;
44 void *NaClContainerListIterStar(struct NaClContainerIter *vself) {
45 struct NaClContainerListIter *self = (struct NaClContainerListIter *) vself;
47 return (*self->cur)->datum;
51 void NaClContainerListIterIncr(struct NaClContainerIter *vself) {
52 struct NaClContainerListIter *self = (struct NaClContainerListIter *) vself;
54 self->cur = &(*self->cur)->next;
58 void NaClContainerListIterErase(struct NaClContainerIter *vself) {
59 struct NaClContainerListIter *self = (struct NaClContainerListIter *) vself;
60 struct NaClItemList *doomed = *self->cur;
61 struct NaClItemList *next = (*self->cur)->next;
69 void *NaClContainerListIterExtract(struct NaClContainerIter *vself) {
70 struct NaClContainerListIter *self = (struct NaClContainerListIter *) vself;
71 struct NaClItemList *doomed = *self->cur;
72 struct NaClItemList *next = (*self->cur)->next;
76 datum = doomed->datum;
81 static struct NaClContainerIterVtbl const kNaClContainerListIterVtbl = {
82 NaClContainerListIterAtEnd,
83 NaClContainerListIterStar,
84 NaClContainerListIterIncr,
85 NaClContainerListIterErase,
86 NaClContainerListIterExtract,
90 int NaClContainerListInsert(struct NaClContainer *vself,
92 struct NaClContainerList *self = (struct NaClContainerList *) vself;
93 struct NaClItemList *new_entry = malloc(sizeof *new_entry);
94 struct NaClItemList **ipp;
95 struct NaClItemList *ip;
100 new_entry->datum = obj;
102 for (ipp = &self->head; 0 != (ip = *ipp); ipp = &ip->next) {
103 if ((*self->cmp_functor->vtbl->OrderCmp)(self->cmp_functor,
104 obj, ip->datum) >= 0) {
109 new_entry->next = ip;
115 struct NaClContainerIter *NaClContainerListFind(
116 struct NaClContainer *vself,
118 struct NaClContainerIter *vout) {
119 struct NaClContainerList *self = (struct NaClContainerList *) vself;
120 struct NaClItemList **ipp;
121 struct NaClItemList *ip;
122 struct NaClContainerListIter *out = (struct NaClContainerListIter *) vout;
124 for (ipp = &self->head; (ip = *ipp) != 0; ipp = &ip->next) {
125 if ((*self->cmp_functor->vtbl->OrderCmp)(self->cmp_functor,
126 key, ip->datum) == 0) {
132 vout->vtbl = &kNaClContainerListIterVtbl;
137 void NaClContainerListDtor(struct NaClContainer *vself) {
138 struct NaClContainerList *self = (struct NaClContainerList *) vself;
139 struct NaClItemList *cur, *next;
141 for (cur = self->head; cur; cur = next) {
149 int NaClContainerListIterCtor(struct NaClContainer *vself,
150 struct NaClContainerIter *viter) {
151 struct NaClContainerList *self = (struct NaClContainerList *) vself;
152 struct NaClContainerListIter *iter = (struct NaClContainerListIter *) viter;
154 iter->cur = &self->head;
156 viter->vtbl = &kNaClContainerListIterVtbl;