2010-08-18 01:40:07 -05:00
|
|
|
/*
|
|
|
|
* The Rust runtime uses memory regions to provide a primitive level of
|
|
|
|
* memory management and isolation between tasks, and domains.
|
|
|
|
*
|
2011-06-27 12:08:57 -05:00
|
|
|
* FIXME: Implement a custom lock-free malloc / free instead of relying solely
|
2010-08-18 01:40:07 -05:00
|
|
|
* on the standard malloc / free.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MEMORY_REGION_H
|
|
|
|
#define MEMORY_REGION_H
|
|
|
|
|
2010-09-08 21:13:49 -05:00
|
|
|
#include "sync/lock_and_signal.h"
|
2010-08-18 01:40:07 -05:00
|
|
|
|
2011-11-07 19:19:29 -06:00
|
|
|
// There are three levels of debugging:
|
|
|
|
//
|
|
|
|
// 0 --- no headers, no debugging support
|
|
|
|
// 1 --- support poison, but do not track allocations
|
|
|
|
// 2 --- track allocations in deatil
|
|
|
|
//
|
|
|
|
// NB: please do not commit code with level 2. It's
|
|
|
|
// hugely expensive and should only be used as a last resort.
|
|
|
|
#define RUSTRT_TRACK_ALLOCATIONS 0
|
|
|
|
|
2010-08-18 01:40:07 -05:00
|
|
|
class rust_srv;
|
|
|
|
|
|
|
|
class memory_region {
|
|
|
|
private:
|
2011-07-18 14:02:26 -05:00
|
|
|
struct alloc_header {
|
2011-11-07 19:19:29 -06:00
|
|
|
# if RUSTRT_TRACK_ALLOCATIONS > 0
|
2011-07-18 14:02:26 -05:00
|
|
|
uint32_t magic;
|
|
|
|
int index;
|
|
|
|
const char *tag;
|
2011-09-06 16:15:01 -05:00
|
|
|
uint32_t size;
|
2011-11-07 19:19:29 -06:00
|
|
|
# endif
|
2011-07-18 14:02:26 -05:00
|
|
|
};
|
|
|
|
|
2011-11-07 19:19:29 -06:00
|
|
|
inline alloc_header *get_header(void *mem);
|
|
|
|
inline void *get_data(alloc_header *);
|
|
|
|
|
2011-07-18 14:02:26 -05:00
|
|
|
|
2010-08-18 01:40:07 -05:00
|
|
|
rust_srv *_srv;
|
|
|
|
memory_region *_parent;
|
2011-08-12 18:36:17 -05:00
|
|
|
int _live_allocations;
|
2011-07-18 14:02:26 -05:00
|
|
|
array_list<alloc_header *> _allocation_list;
|
2010-11-30 19:10:51 -06:00
|
|
|
const bool _detailed_leaks;
|
2010-08-18 01:40:07 -05:00
|
|
|
const bool _synchronized;
|
2010-09-08 21:13:49 -05:00
|
|
|
lock_and_signal _lock;
|
2011-07-07 13:53:08 -05:00
|
|
|
|
|
|
|
void add_alloc();
|
|
|
|
void dec_alloc();
|
2011-09-06 16:15:01 -05:00
|
|
|
void maybe_poison(void *mem);
|
|
|
|
|
2010-08-18 01:40:07 -05:00
|
|
|
public:
|
|
|
|
memory_region(rust_srv *srv, bool synchronized);
|
|
|
|
memory_region(memory_region *parent);
|
2011-07-18 14:02:26 -05:00
|
|
|
void *malloc(size_t size, const char *tag, bool zero = true);
|
|
|
|
void *calloc(size_t size, const char *tag);
|
2010-08-18 01:40:07 -05:00
|
|
|
void *realloc(void *mem, size_t size);
|
|
|
|
void free(void *mem);
|
|
|
|
virtual ~memory_region();
|
2011-08-12 18:36:17 -05:00
|
|
|
|
|
|
|
void release_alloc(void *mem);
|
|
|
|
void claim_alloc(void *mem);
|
2010-08-18 01:40:07 -05:00
|
|
|
};
|
|
|
|
|
2011-07-18 14:02:26 -05:00
|
|
|
inline void *operator new(size_t size, memory_region ®ion,
|
|
|
|
const char *tag) {
|
|
|
|
return region.malloc(size, tag);
|
2010-09-07 20:09:52 -05:00
|
|
|
}
|
|
|
|
|
2011-07-18 14:02:26 -05:00
|
|
|
inline void *operator new(size_t size, memory_region *region,
|
|
|
|
const char *tag) {
|
|
|
|
return region->malloc(size, tag);
|
2010-09-07 20:09:52 -05:00
|
|
|
}
|
|
|
|
|
2010-09-08 21:13:49 -05:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: C++
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|
|
|
|
//
|
|
|
|
|
2010-08-18 01:40:07 -05:00
|
|
|
#endif /* MEMORY_REGION_H */
|