+ for (Cleaner *cleaner(cleaner_); cleaner != NULL; ) {
+ Cleaner *next(cleaner->next_);
+ (*cleaner->code_)(cleaner->data_);
+ cleaner = next;
+ }
+ }
+
+ template <typename Type_>
+ Type_ *malloc(size_t size) {
+ size = align(size);
+
+ if (size > size_) {
+ // XXX: is this an optimal malloc size?
+ size_ = std::max<size_t>(size, size + align(sizeof(Cleaner)));
+ data_ = reinterpret_cast<uint8_t *>(::malloc(size_));
+ atexit(free, data_);
+ _assert(size <= size_);
+ }
+
+ void *data(data_);
+ data_ += size;
+ size_ -= size;
+ return reinterpret_cast<Type_ *>(data);
+ }
+
+ char *strdup(const char *data) {
+ if (data == NULL)
+ return NULL;
+ return reinterpret_cast<char *>(memdup(data, strlen(data) + 1));
+ }
+
+ void *memdup(const void *data, size_t size) {
+ void *copy(malloc<void>(size));
+ memcpy(copy, data, size);
+ return copy;
+ }
+
+ char *strndup(const char *data, size_t size) const {
+ return strmemdup(data, strnlen(data, size));