memory_region.h 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

11 12 13 14
/*
 * The Rust runtime uses memory regions to provide a primitive level of
 * memory management and isolation between tasks, and domains.
 *
15 16
 * FIXME (#2686): Implement a custom lock-free malloc / free instead of
 *       relying solely on the standard malloc / free.
17 18 19 20 21
 */

#ifndef MEMORY_REGION_H
#define MEMORY_REGION_H

22
#include "rust_globals.h"
23
#include "sync/lock_and_signal.h"
24
#include "util/array_list.h"
25

26 27 28 29
// There are three levels of debugging:
//
// 0 --- no headers, no debugging support
// 1 --- support poison, but do not track allocations
N
Niko Matsakis 已提交
30
// 2 --- track allocations in detail
31
// 3 --- record backtraces of every allocation
32 33 34 35 36
//
// 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

B
Brian Anderson 已提交
37
struct rust_env;
38 39 40

class memory_region {
private:
41
    struct alloc_header {
42
#       if RUSTRT_TRACK_ALLOCATIONS > 0
43 44 45
        uint32_t magic;
        int index;
        const char *tag;
46
        uint32_t size;
47 48 49 50
#       if RUSTRT_TRACK_ALLOCATIONS >= 3
        void *bt[32];
        int btframes;
#       endif
51
#       endif
52 53
    };

54 55
    inline alloc_header *get_header(void *mem);
    inline void *get_data(alloc_header *);
56

57
    memory_region *_parent;
58
    int _live_allocations;
59
    array_list<alloc_header *> _allocation_list;
60
    const bool _detailed_leaks;
61
    const bool _poison_on_free;
62
    lock_and_signal _lock;
E
Eric Holk 已提交
63 64 65

    void add_alloc();
    void dec_alloc();
66 67
    void maybe_poison(void *mem);

68 69 70
    void release_alloc(void *mem);
    void claim_alloc(void *mem);

71 72
    void maybe_print_backtrace(const alloc_header *) const;

73 74 75 76
private:
    // private and undefined to disable copying
    memory_region(const memory_region& rhs);
    memory_region& operator=(const memory_region& rhs);
B
Brian Anderson 已提交
77

78
public:
79
    memory_region(bool detailed_leaks, bool poison_on_free);
80
    memory_region(memory_region *parent);
81
    void *malloc(size_t size, const char *tag);
82 83
    void *realloc(void *mem, size_t size);
    void free(void *mem);
84
    ~memory_region();
85
 };
86

87 88 89
inline void *operator new(size_t size, memory_region &region,
                          const char *tag) {
    return region.malloc(size, tag);
90 91
}

92 93 94
inline void *operator new(size_t size, memory_region *region,
                          const char *tag) {
    return region->malloc(size, tag);
95 96
}

97 98 99 100 101 102 103 104 105 106
//
// Local Variables:
// mode: C++
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
//

107
#endif /* MEMORY_REGION_H */