array.c++ 3.97 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// Copyright (c) 2013, Kenton Varda <temporal@gmail.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "array.h"
25
#include "exception.h"
26 27

namespace kj {
Kenton Varda's avatar
Kenton Varda committed
28

29 30 31 32 33 34
void ExceptionSafeArrayUtil::construct(size_t count, void (*constructElement)(void*)) {
  while (count > 0) {
    constructElement(pos);
    pos += elementSize;
    ++constructedElementCount;
    --count;
Kenton Varda's avatar
Kenton Varda committed
35
  }
36
}
Kenton Varda's avatar
Kenton Varda committed
37

38 39 40 41 42
void ExceptionSafeArrayUtil::destroyAll() {
  while (constructedElementCount > 0) {
    pos -= elementSize;
    --constructedElementCount;
    destroyElement(pos);
Kenton Varda's avatar
Kenton Varda committed
43
  }
44 45
}

46 47 48 49 50 51 52 53 54 55 56 57
const DestructorOnlyArrayDisposer DestructorOnlyArrayDisposer::instance =
    DestructorOnlyArrayDisposer();

void DestructorOnlyArrayDisposer::disposeImpl(
    void* firstElement, size_t elementSize, size_t elementCount,
    size_t capacity, void (*destroyElement)(void*)) const {
  if (destroyElement != nullptr) {
    ExceptionSafeArrayUtil guard(firstElement, elementSize, elementCount, destroyElement);
    guard.destroyAll();
  }
}

58 59 60 61 62 63
const NullArrayDisposer NullArrayDisposer::instance = NullArrayDisposer();

void NullArrayDisposer::disposeImpl(
    void* firstElement, size_t elementSize, size_t elementCount,
    size_t capacity, void (*destroyElement)(void*)) const {}

64
namespace _ {  // private
Kenton Varda's avatar
Kenton Varda committed
65

66 67 68 69 70 71 72
struct AutoDeleter {
  void* ptr;
  inline void* release() { void* result = ptr; ptr = nullptr; return result; }
  inline AutoDeleter(void* ptr): ptr(ptr) {}
  inline ~AutoDeleter() { operator delete(ptr); }
};

Kenton Varda's avatar
Kenton Varda committed
73 74 75
void* HeapArrayDisposer::allocateImpl(size_t elementSize, size_t elementCount, size_t capacity,
                                      void (*constructElement)(void*),
                                      void (*destroyElement)(void*)) {
76
  AutoDeleter result(operator new(elementSize * capacity));
Kenton Varda's avatar
Kenton Varda committed
77 78 79 80

  if (constructElement == nullptr) {
    // Nothing to do.
  } else if (destroyElement == nullptr) {
81
    byte* pos = reinterpret_cast<byte*>(result.ptr);
Kenton Varda's avatar
Kenton Varda committed
82 83 84 85 86 87
    while (elementCount > 0) {
      constructElement(pos);
      pos += elementSize;
      --elementCount;
    }
  } else {
88
    ExceptionSafeArrayUtil guard(result.ptr, elementSize, 0, destroyElement);
89 90
    guard.construct(elementCount, constructElement);
    guard.release();
Kenton Varda's avatar
Kenton Varda committed
91 92
  }

93
  return result.release();
Kenton Varda's avatar
Kenton Varda committed
94 95 96 97 98 99
}

void HeapArrayDisposer::disposeImpl(
    void* firstElement, size_t elementSize, size_t elementCount, size_t capacity,
    void (*destroyElement)(void*)) const {
  // Note that capacity is ignored since operator delete() doesn't care about it.
100
  AutoDeleter deleter(firstElement);
Kenton Varda's avatar
Kenton Varda committed
101

102
  if (destroyElement != nullptr) {
103
    ExceptionSafeArrayUtil guard(firstElement, elementSize, elementCount, destroyElement);
Kenton Varda's avatar
Kenton Varda committed
104 105 106 107 108 109
    guard.destroyAll();
  }
}

const HeapArrayDisposer HeapArrayDisposer::instance = HeapArrayDisposer();

110
}  // namespace _ (private)
111
}  // namespace kj