3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-07-23 20:58:54 +00:00
z3/src/util/small_object_allocator.h
Nikolaj Bjorner cfa7c733db
fixing #4670 (#4682)
* fixing #4670

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* init

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* arrays

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* arrays

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* arrays

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* na

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2020-09-10 04:35:11 -07:00

58 lines
1.6 KiB
C++

/*++
Copyright (c) 2006 Microsoft Corporation
Module Name:
small_object_allocator.h
Abstract:
Small object allocator.
Author:
Nikolaj bjorner (nbjorner) 2007-08-06.
Revision History:
Leonardo de Moura (leonardo) 2011-04-27
Rewrote/Simplified the allocator
--*/
#pragma once
#include "util/machine.h"
#include "util/debug.h"
class small_object_allocator {
static const unsigned CHUNK_SIZE = (8192 - sizeof(void*)*2);
static const unsigned SMALL_OBJ_SIZE = 256;
static const unsigned NUM_SLOTS = (SMALL_OBJ_SIZE >> PTR_ALIGNMENT);
struct chunk {
chunk* m_next{ nullptr };
char* m_curr{ nullptr };
char m_data[CHUNK_SIZE];
chunk():m_curr(m_data) {}
};
chunk * m_chunks[NUM_SLOTS];
void * m_free_list[NUM_SLOTS];
size_t m_alloc_size;
#ifdef Z3DEBUG
char const * m_id;
#endif
public:
small_object_allocator(char const * id = "unknown");
~small_object_allocator();
void reset();
void * allocate(size_t size);
void deallocate(size_t size, void * p);
size_t get_allocation_size() const { return m_alloc_size; }
size_t get_wasted_size() const;
size_t get_num_free_objs() const;
void consolidate();
};
inline void * operator new(size_t s, small_object_allocator & r) { return r.allocate(s); }
inline void * operator new[](size_t s, small_object_allocator & r) { return r.allocate(s); }
inline void operator delete(void * p, small_object_allocator & r) { UNREACHABLE(); }
inline void operator delete[](void * p, small_object_allocator & r) { UNREACHABLE(); }