1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/List.h Tue Jan 01 15:34:07 2013 -0800
1.3 @@ -0,0 +1,119 @@
1.4 +/* vim: set ts=4 sw=4 tw=99 et:
1.5 + *
1.6 + * Copyright (C) 2012 David Anderson
1.7 + *
1.8 + * This file is part of SourcePawn.
1.9 + *
1.10 + * SourcePawn is free software: you can redistribute it and/or modify it under
1.11 + * the terms of the GNU General Public License as published by the Free
1.12 + * Software Foundation, either version 3 of the License, or (at your option)
1.13 + * any later version.
1.14 + *
1.15 + * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
1.16 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License along with
1.20 + * SourcePawn. If not, see http://www.gnu.org/licenses/.
1.21 + */
1.22 +#ifndef _include_sp2_list_h_
1.23 +#define _include_sp2_list_h_
1.24 +
1.25 +#include "Objects.h"
1.26 +#include "FixedArray.h"
1.27 +#include "Zone.h"
1.28 +#include "Heap-inl.h"
1.29 +#include "Handles-inl.h"
1.30 +
1.31 +namespace ke {
1.32 +
1.33 +template <typename T>
1.34 +class List : public Object
1.35 +{
1.36 + public:
1.37 + static List *New(Zone *zone, Heap::Tenure tenure = Heap::Tenure_Default) {
1.38 + Local<List> list(zone, List::cast(zone->allocate(MapKind_List, sizeof(List), tenure)));
1.39 + if (!list)
1.40 + return NULL;
1.41 +
1.42 + list->length_ = 0;
1.43 + list->array_ = NULL;
1.44 +
1.45 + return list;
1.46 + }
1.47 +
1.48 + bool append(Zone *zone, Handle<T> obj) {
1.49 + Local<List> list(zone, this);
1.50 + if (!Grow(zone, list, 1))
1.51 + return false;
1.52 +
1.53 + list->array_->set(zone, list->length_, obj);
1.54 + list->length_++;
1.55 + return true;
1.56 + }
1.57 +
1.58 + T *at(unsigned i) {
1.59 + return T::cast(array_->at(i));
1.60 + }
1.61 +
1.62 + unsigned length() {
1.63 + return length_;
1.64 + }
1.65 +
1.66 + private:
1.67 + static bool Grow(Zone *zone, Handle<List> list, unsigned amount) {
1.68 + if (!IsUint32AddSafe(list->length(), amount)) {
1.69 + zone->reportAllocationOverflow();
1.70 + return false;
1.71 + }
1.72 +
1.73 + unsigned newlen = list->length() + amount;
1.74 + if (list->array_ && newlen < list->array_->length())
1.75 + return true;
1.76 +
1.77 + unsigned maxsize = list->array_ ? list->array_->length() : 8;
1.78 + if (!IsUint32AddSafe(maxsize, amount)) {
1.79 + zone->reportAllocationOverflow();
1.80 + return false;
1.81 + }
1.82 +
1.83 + while (newlen > maxsize) {
1.84 + if (!IsUint32MultiplySafe(maxsize, 2)) {
1.85 + zone->reportAllocationOverflow();
1.86 + return false;
1.87 + }
1.88 + maxsize *= 2;
1.89 + }
1.90 +
1.91 + Local<FixedArray> array(zone, FixedArray::New(zone, maxsize, Heap::Tenure_Default));
1.92 + if (!array)
1.93 + return false;
1.94 +
1.95 + for (unsigned i = 0; i < list->length_; i++)
1.96 + array->set(zone, i, list->array_->at(i));
1.97 +
1.98 + list->array_ = array;
1.99 + return true;
1.100 + }
1.101 +
1.102 + public:
1.103 +
1.104 + // Begin GC descriptor.
1.105 + static inline size_t offsetOfArray() {
1.106 + return OFFSETOF(List, array_);
1.107 + }
1.108 + // End GC descriptor.
1.109 +
1.110 + static inline List *cast(Object *obj) {
1.111 + assert(!obj || obj->is(MapKind_List));
1.112 + return reinterpret_cast<List *>(obj);
1.113 + }
1.114 +
1.115 + private:
1.116 + unsigned length_;
1.117 + FixedArray *array_;
1.118 +};
1.119 +
1.120 +} // namespace ke
1.121 +
1.122 +#endif // _include_sp2_list_h_