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
|
2011-12-13 23:43:55 -06:00
|
|
|
// 2 --- track allocations in detail
|
2012-03-26 20:58:40 -05:00
|
|
|
// 3 --- record backtraces of every allocation
|
2011-11-07 19:19:29 -06:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2012-04-01 22:18:40 -05:00
|
|
|
class rust_env;
|
2010-08-18 01:40:07 -05:00
|
|
|
|
|
|
|
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;
|
2012-03-26 20:58:40 -05:00
|
|
|
# if RUSTRT_TRACK_ALLOCATIONS >= 3
|
|
|
|
void *bt[32];
|
|
|
|
int btframes;
|
|
|
|
# endif
|
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
|
|
|
|
2012-04-01 22:18:40 -05:00
|
|
|
rust_env *_env;
|
2010-08-18 01:40:07 -05:00
|
|
|
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);
|
|
|
|
|
2012-02-01 20:52:08 -06:00
|
|
|
void release_alloc(void *mem);
|
|
|
|
void claim_alloc(void *mem);
|
|
|
|
|
2010-08-18 01:40:07 -05:00
|
|
|
public:
|
2012-04-01 22:18:40 -05:00
|
|
|
memory_region(rust_env *env, bool synchronized);
|
2010-08-18 01:40:07 -05:00
|
|
|
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);
|
2012-03-05 16:47:24 -06:00
|
|
|
~memory_region();
|
2012-02-01 20:52:08 -06:00
|
|
|
};
|
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 */
|