about summary refs log tree commit diff
path: root/src/rt
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2013-10-26 01:10:39 -0700
committerAlex Crichton <alex@alexcrichton.com>2013-10-26 01:10:39 -0700
commit357ef1f69cd1ffbd3c36a9e7e171b10263d94139 (patch)
treefdbf34015e80a82799979b60454dcf9c01cafb56 /src/rt
parentc5074ae6463876bbb9511e980fe890bebc881e41 (diff)
downloadrust-357ef1f69cd1ffbd3c36a9e7e171b10263d94139.tar.gz
rust-357ef1f69cd1ffbd3c36a9e7e171b10263d94139.zip
Rewrite boxed_region/memory_region in Rust
This drops more of the old C++ runtime to rather be written in rust. A few
features were lost along the way, but hopefully not too many. The main loss is
that there are no longer backtraces associated with allocations (rust doesn't
have a way of acquiring those just yet). Other than that though, I believe that
the rest of the debugging utilities made their way over into rust.

Closes #8704
Diffstat (limited to 'src/rt')
-rw-r--r--src/rt/boxed_region.cpp99
-rw-r--r--src/rt/boxed_region.h68
-rw-r--r--src/rt/memory_region.cpp258
-rw-r--r--src/rt/memory_region.h107
-rw-r--r--src/rt/rust_builtin.cpp40
-rw-r--r--src/rt/rustrt.def.in7
6 files changed, 0 insertions, 579 deletions
diff --git a/src/rt/boxed_region.cpp b/src/rt/boxed_region.cpp
deleted file mode 100644
index 064648a9671..00000000000
--- a/src/rt/boxed_region.cpp
+++ /dev/null
@@ -1,99 +0,0 @@
-// 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.
-
-#include "memory_region.h"
-#include "boxed_region.h"
-#include "rust_globals.h"
-#include "rust_util.h"
-
-// #define DUMP_BOXED_REGION
-
-rust_opaque_box *boxed_region::malloc(type_desc *td, size_t body_size) {
-    size_t total_size = get_box_size(body_size, td->align);
-    rust_opaque_box *box =
-      (rust_opaque_box*)backing_region->malloc(total_size, "@");
-    box->td = td;
-    box->ref_count = 1;
-    box->prev = NULL;
-    box->next = live_allocs;
-    if (live_allocs) live_allocs->prev = box;
-    live_allocs = box;
-
-    /*LOG(rust_get_current_task(), box,
-        "@malloc()=%p with td %p, size %lu==%lu+%lu, "
-        "align %lu, prev %p, next %p\n",
-        box, td, total_size, sizeof(rust_opaque_box), body_size,
-        td->align, box->prev, box->next);*/
-
-    return box;
-}
-
-rust_opaque_box *boxed_region::realloc(rust_opaque_box *box,
-                                       size_t new_size) {
-
-    size_t total_size = new_size + sizeof(rust_opaque_box);
-    rust_opaque_box *new_box =
-        (rust_opaque_box*)backing_region->realloc(box, total_size);
-    if (new_box->prev) new_box->prev->next = new_box;
-    if (new_box->next) new_box->next->prev = new_box;
-    if (live_allocs == box) live_allocs = new_box;
-
-    /*LOG(rust_get_current_task(), box,
-        "@realloc()=%p with orig=%p, size %lu==%lu+%lu",
-        new_box, box, total_size, sizeof(rust_opaque_box), new_size);*/
-
-    return new_box;
-}
-
-
-rust_opaque_box *boxed_region::calloc(type_desc *td, size_t body_size) {
-    rust_opaque_box *box = malloc(td, body_size);
-    memset(box_body(box), 0, td->size);
-    return box;
-}
-
-void boxed_region::free(rust_opaque_box *box) {
-    // This turns out to not be true in various situations,
-    // like when we are unwinding after a failure.
-    //
-    // assert(box->ref_count == 0);
-
-    // This however should always be true.  Helps to detect
-    // double frees (kind of).
-    assert(box->td != NULL);
-
-    /*LOG(rust_get_current_task(), box,
-        "@free(%p) with td %p, prev %p, next %p\n",
-        box, box->td, box->prev, box->next);*/
-
-    if (box->prev) box->prev->next = box->next;
-    if (box->next) box->next->prev = box->prev;
-    if (live_allocs == box) live_allocs = box->next;
-
-    if (poison_on_free) {
-        memset(box_body(box), 0xab, box->td->size);
-    }
-
-    box->prev = NULL;
-    box->next = NULL;
-    box->td = NULL;
-
-    backing_region->free(box);
-}
-
-//
-// Local Variables:
-// mode: C++
-// fill-column: 78;
-// indent-tabs-mode: nil
-// c-basic-offset: 4
-// buffer-file-coding-system: utf-8-unix
-// End:
-//
diff --git a/src/rt/boxed_region.h b/src/rt/boxed_region.h
deleted file mode 100644
index 178772007e5..00000000000
--- a/src/rt/boxed_region.h
+++ /dev/null
@@ -1,68 +0,0 @@
-// 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.
-
-#ifndef BOXED_REGION_H
-#define BOXED_REGION_H
-
-#include <stdlib.h>
-
-struct type_desc;
-class memory_region;
-struct rust_opaque_box;
-struct rust_env;
-
-/* Tracks the data allocated by a particular task in the '@' region.
- * Currently still relies on the standard malloc as a backing allocator, but
- * this could be improved someday if necessary. Every allocation must provide
- * a type descr which describes the payload (what follows the header). */
-class boxed_region {
-private:
-    bool poison_on_free;
-    memory_region *backing_region;
-    rust_opaque_box *live_allocs;
-
-    size_t align_to(size_t v, size_t align) {
-        size_t alignm1 = align - 1;
-        v += alignm1;
-        v &= ~alignm1;
-        return v;
-    }
-
-private:
-    // private and undefined to disable copying
-    boxed_region(const boxed_region& rhs);
-    boxed_region& operator=(const boxed_region& rhs);
-
-public:
-    boxed_region(memory_region *br, bool poison_on_free)
-        : poison_on_free(poison_on_free)
-        , backing_region(br)
-        , live_allocs(NULL)
-    {}
-
-    rust_opaque_box *first_live_alloc() { return live_allocs; }
-
-    rust_opaque_box *malloc(type_desc *td, size_t body_size);
-    rust_opaque_box *calloc(type_desc *td, size_t body_size);
-    rust_opaque_box *realloc(rust_opaque_box *box, size_t new_size);
-    void free(rust_opaque_box *box);
-};
-
-#endif /* BOXED_REGION_H */
-
-//
-// Local Variables:
-// mode: C++
-// fill-column: 78;
-// indent-tabs-mode: nil
-// c-basic-offset: 4
-// buffer-file-coding-system: utf-8-unix
-// End:
-//
diff --git a/src/rt/memory_region.cpp b/src/rt/memory_region.cpp
deleted file mode 100644
index 4a34312c6d4..00000000000
--- a/src/rt/memory_region.cpp
+++ /dev/null
@@ -1,258 +0,0 @@
-// 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.
-
-
-#include "memory_region.h"
-
-#if RUSTRT_TRACK_ALLOCATIONS >= 3
-#include <execinfo.h>
-#endif
-
-#if RUSTRT_TRACK_ALLOCATIONS >= 1
-// For some platforms, 16 byte alignment is required.
-#  define PTR_SIZE 16
-#  define ALIGN_PTR(x) (((x)+PTR_SIZE-1)/PTR_SIZE*PTR_SIZE)
-#  define HEADER_SIZE ALIGN_PTR(sizeof(alloc_header))
-#  define MAGIC 0xbadc0ffe
-#else
-#  define HEADER_SIZE 0
-#endif
-
-memory_region::alloc_header *memory_region::get_header(void *mem) {
-    return (alloc_header *)((char *)mem - HEADER_SIZE);
-}
-
-void *memory_region::get_data(alloc_header *ptr) {
-    return (void*)((char *)ptr + HEADER_SIZE);
-}
-
-inline void memory_region::maybe_print_backtrace(const alloc_header *header) const {
-#   if RUSTRT_TRACK_ALLOCATIONS >= 3
-    if (_detailed_leaks) {
-        backtrace_symbols_fd(header->bt + 1, header->btframes - 1, 2);
-    }
-#   endif
-}
-
-memory_region::memory_region(bool detailed_leaks,
-                             bool poison_on_free) :
-    _parent(NULL), _live_allocations(0),
-    _detailed_leaks(detailed_leaks),
-    _poison_on_free(poison_on_free) {
-}
-
-memory_region::memory_region(memory_region *parent) :
-    _parent(parent), _live_allocations(0),
-    _detailed_leaks(parent->_detailed_leaks),
-    _poison_on_free(parent->_poison_on_free) {
-}
-
-void memory_region::add_alloc() {
-    _live_allocations++;
-}
-
-void memory_region::dec_alloc() {
-    _live_allocations--;
-}
-
-void memory_region::free(void *mem) {
-    // printf("free: ptr 0x%" PRIxPTR" region=%p\n", (uintptr_t) mem, this);
-    if (!mem) { return; }
-    alloc_header *alloc = get_header(mem);
-
-#   if RUSTRT_TRACK_ALLOCATIONS >= 1
-    assert(alloc->magic == MAGIC);
-#   endif
-
-    if (_live_allocations < 1) {
-        assert(false && "live_allocs < 1");
-    }
-    release_alloc(mem);
-    maybe_poison(mem);
-    ::free(alloc);
-}
-
-void *
-memory_region::realloc(void *mem, size_t orig_size) {
-    if (!mem) {
-        add_alloc();
-    }
-
-    alloc_header *alloc = get_header(mem);
-#   if RUSTRT_TRACK_ALLOCATIONS >= 1
-    assert(alloc->magic == MAGIC);
-#   endif
-
-    size_t size = orig_size + HEADER_SIZE;
-    alloc_header *newMem = (alloc_header *)::realloc(alloc, size);
-    if (newMem == NULL) {
-        fprintf(stderr,
-                "memory_region::realloc> "
-                "Out of memory allocating %ld bytes",
-                (long int) size);
-        abort();
-    }
-
-#   if RUSTRT_TRACK_ALLOCATIONS >= 1
-    assert(newMem->magic == MAGIC);
-    newMem->size = orig_size;
-#   endif
-
-#   if RUSTRT_TRACK_ALLOCATIONS >= 2
-    if (_allocation_list[newMem->index] != alloc) {
-        printf("at index %d, found %p, expected %p\n",
-               alloc->index, _allocation_list[alloc->index], alloc);
-        printf("realloc: ptr 0x%" PRIxPTR " (%s) is not in allocation_list\n",
-               (uintptr_t) get_data(alloc), alloc->tag);
-        assert(false && "not in allocation_list");
-    }
-    else {
-        _allocation_list[newMem->index] = newMem;
-        // printf("realloc: stored %p at index %d, replacing %p\n",
-        //        newMem, index, mem);
-    }
-#   endif
-
-    return get_data(newMem);
-}
-
-void *
-memory_region::malloc(size_t size, const char *tag) {
-#   if RUSTRT_TRACK_ALLOCATIONS >= 1
-    size_t old_size = size;
-#   endif
-    size += HEADER_SIZE;
-    alloc_header *mem = (alloc_header *)::malloc(size);
-    if (mem == NULL) {
-        fprintf(stderr,
-                "memory_region::malloc> "
-                "Out of memory allocating %ld bytes",
-                (long int) size);
-        abort();
-    }
-
-#   if RUSTRT_TRACK_ALLOCATIONS >= 1
-    mem->magic = MAGIC;
-    mem->tag = tag;
-    mem->index = -1;
-    mem->size = old_size;
-#   endif
-
-    void *data = get_data(mem);
-    claim_alloc(data);
-
-    return data;
-}
-
-memory_region::~memory_region() {
-    if (_live_allocations == 0 && !_detailed_leaks) {
-        return;
-    }
-    char msg[128];
-    if(_live_allocations > 0) {
-        snprintf(msg, sizeof(msg),
-                 "leaked memory in rust main loop (%d objects)",
-                 _live_allocations);
-    }
-
-#   if RUSTRT_TRACK_ALLOCATIONS >= 2
-    if (_detailed_leaks) {
-        int leak_count = 0;
-        for (size_t i = 0; i < _allocation_list.size(); i++) {
-            if (_allocation_list[i] != NULL) {
-                alloc_header *header = (alloc_header*)_allocation_list[i];
-                printf("allocation (%s) 0x%" PRIxPTR " was not freed\n",
-                       header->tag,
-                       (uintptr_t) get_data(header));
-                ++leak_count;
-                maybe_print_backtrace(header);
-            }
-        }
-        assert(leak_count == _live_allocations);
-    }
-#   endif
-
-    if (_live_allocations > 0) {
-        fprintf(stderr, "%s\n", msg);
-        assert(false);
-    }
-}
-
-void
-memory_region::release_alloc(void *mem) {
-#   if RUSTRT_TRACK_ALLOCATIONS >= 1
-    alloc_header *alloc = get_header(mem);
-    assert(alloc->magic == MAGIC);
-#   endif
-
-#   if RUSTRT_TRACK_ALLOCATIONS >= 2
-    if (((size_t) alloc->index) >= _allocation_list.size()) {
-        printf("free: ptr 0x%" PRIxPTR " (%s) index %d is beyond allocation_list of size %zu\n",
-               (uintptr_t) get_data(alloc), alloc->tag, alloc->index, _allocation_list.size());
-        maybe_print_backtrace(alloc);
-        assert(false && "index beyond allocation_list");
-    }
-    if (_allocation_list[alloc->index] != alloc) {
-        printf("free: ptr 0x%" PRIxPTR " (%s) is not in allocation_list\n",
-               (uintptr_t) get_data(alloc), alloc->tag);
-        maybe_print_backtrace(alloc);
-        assert(false && "not in allocation_list");
-    }
-    else {
-        // printf("freed index %d\n", index);
-        _allocation_list[alloc->index] = NULL;
-        alloc->index = -1;
-    }
-#   endif
-
-    dec_alloc();
-}
-
-void
-memory_region::claim_alloc(void *mem) {
-#   if RUSTRT_TRACK_ALLOCATIONS >= 1
-    alloc_header *alloc = get_header(mem);
-    assert(alloc->magic == MAGIC);
-#   endif
-
-#   if RUSTRT_TRACK_ALLOCATIONS >= 2
-    alloc->index = _allocation_list.append(alloc);
-#   endif
-
-#   if RUSTRT_TRACK_ALLOCATIONS >= 3
-    if (_detailed_leaks) {
-        alloc->btframes = ::backtrace(alloc->bt, 32);
-    }
-#   endif
-
-    add_alloc();
-}
-
-void
-memory_region::maybe_poison(void *mem) {
-
-    if (!_poison_on_free)
-        return;
-
-#   if RUSTRT_TRACK_ALLOCATIONS >= 1
-    alloc_header *alloc = get_header(mem);
-    memset(mem, '\xcd', alloc->size);
-#   endif
-}
-
-//
-// Local Variables:
-// mode: C++
-// fill-column: 78;
-// indent-tabs-mode: nil
-// c-basic-offset: 4
-// buffer-file-coding-system: utf-8-unix
-// End:
-//
diff --git a/src/rt/memory_region.h b/src/rt/memory_region.h
deleted file mode 100644
index ace463ede21..00000000000
--- a/src/rt/memory_region.h
+++ /dev/null
@@ -1,107 +0,0 @@
-// 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.
-
-/*
- * The Rust runtime uses memory regions to provide a primitive level of
- * memory management and isolation between tasks, and domains.
- *
- * FIXME (#2686): Implement a custom lock-free malloc / free instead of
- *       relying solely on the standard malloc / free.
- */
-
-#ifndef MEMORY_REGION_H
-#define MEMORY_REGION_H
-
-#include "rust_globals.h"
-#include "sync/lock_and_signal.h"
-#include "util/array_list.h"
-
-// There are three levels of debugging:
-//
-// 0 --- no headers, no debugging support
-// 1 --- support poison, but do not track allocations
-// 2 --- track allocations in detail
-// 3 --- record backtraces of every allocation
-//
-// 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
-
-struct rust_env;
-
-class memory_region {
-private:
-    struct alloc_header {
-#       if RUSTRT_TRACK_ALLOCATIONS > 0
-        uint32_t magic;
-        int index;
-        const char *tag;
-        uint32_t size;
-#       if RUSTRT_TRACK_ALLOCATIONS >= 3
-        void *bt[32];
-        int btframes;
-#       endif
-#       endif
-    };
-
-    inline alloc_header *get_header(void *mem);
-    inline void *get_data(alloc_header *);
-
-    memory_region *_parent;
-    int _live_allocations;
-    array_list<alloc_header *> _allocation_list;
-    const bool _detailed_leaks;
-    const bool _poison_on_free;
-    lock_and_signal _lock;
-
-    void add_alloc();
-    void dec_alloc();
-    void maybe_poison(void *mem);
-
-    void release_alloc(void *mem);
-    void claim_alloc(void *mem);
-
-    void maybe_print_backtrace(const alloc_header *) const;
-
-private:
-    // private and undefined to disable copying
-    memory_region(const memory_region& rhs);
-    memory_region& operator=(const memory_region& rhs);
-
-public:
-    memory_region(bool detailed_leaks, bool poison_on_free);
-    memory_region(memory_region *parent);
-    void *malloc(size_t size, const char *tag);
-    void *realloc(void *mem, size_t size);
-    void free(void *mem);
-    ~memory_region();
- };
-
-inline void *operator new(size_t size, memory_region &region,
-                          const char *tag) {
-    return region.malloc(size, tag);
-}
-
-inline void *operator new(size_t size, memory_region *region,
-                          const char *tag) {
-    return region->malloc(size, tag);
-}
-
-//
-// Local Variables:
-// mode: C++
-// fill-column: 78;
-// indent-tabs-mode: nil
-// c-basic-offset: 4
-// buffer-file-coding-system: utf-8-unix
-// End:
-//
-
-#endif /* MEMORY_REGION_H */
diff --git a/src/rt/rust_builtin.cpp b/src/rt/rust_builtin.cpp
index a8eec52943e..90cfd98bc48 100644
--- a/src/rt/rust_builtin.cpp
+++ b/src/rt/rust_builtin.cpp
@@ -13,8 +13,6 @@
 #include "rust_util.h"
 #include "sync/rust_thread.h"
 #include "sync/lock_and_signal.h"
-#include "memory_region.h"
-#include "boxed_region.h"
 #include "vg/valgrind.h"
 
 #include <time.h>
@@ -477,44 +475,6 @@ rust_initialize_rt_tls_key(tls_key *key) {
     }
 }
 
-extern "C" CDECL memory_region*
-rust_new_memory_region(uintptr_t detailed_leaks,
-                       uintptr_t poison_on_free) {
-    return new memory_region((bool)detailed_leaks,
-                             (bool)poison_on_free);
-}
-
-extern "C" CDECL void
-rust_delete_memory_region(memory_region *region) {
-    delete region;
-}
-
-extern "C" CDECL boxed_region*
-rust_new_boxed_region(memory_region *region,
-                      uintptr_t poison_on_free) {
-    return new boxed_region(region, poison_on_free);
-}
-
-extern "C" CDECL void
-rust_delete_boxed_region(boxed_region *region) {
-    delete region;
-}
-
-extern "C" CDECL rust_opaque_box*
-rust_boxed_region_malloc(boxed_region *region, type_desc *td, size_t size) {
-    return region->malloc(td, size);
-}
-
-extern "C" CDECL rust_opaque_box*
-rust_boxed_region_realloc(boxed_region *region, rust_opaque_box *ptr, size_t size) {
-    return region->realloc(ptr, size);
-}
-
-extern "C" CDECL void
-rust_boxed_region_free(boxed_region *region, rust_opaque_box *box) {
-    region->free(box);
-}
-
 typedef void *(rust_try_fn)(void*, void*);
 
 extern "C" CDECL uintptr_t
diff --git a/src/rt/rustrt.def.in b/src/rt/rustrt.def.in
index 06f4c0006f1..47c9554daa0 100644
--- a/src/rt/rustrt.def.in
+++ b/src/rt/rustrt.def.in
@@ -164,13 +164,6 @@ rust_uv_free_ip4_addr
 rust_uv_free_ip6_addr
 rust_initialize_rt_tls_key
 rust_dbg_next_port
-rust_new_memory_region
-rust_delete_memory_region
-rust_new_boxed_region
-rust_delete_boxed_region
-rust_boxed_region_malloc
-rust_boxed_region_realloc
-rust_boxed_region_free
 rust_try
 rust_begin_unwind
 rust_valgrind_stack_register