Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/memory #4145

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/rt/boxed_region.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class boxed_region {
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(rust_env *e, memory_region *br)
: env(e)
Expand Down
5 changes: 5 additions & 0 deletions src/rt/circular_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ circular_buffer : public kernel_owned<circular_buffer> {
bool is_empty();
size_t size();

private:
// private and undefined to disable copying
circular_buffer(const circular_buffer& rhs);
circular_buffer& operator=(const circular_buffer& rhs);

private:
size_t initial_size();
void grow();
Expand Down
8 changes: 8 additions & 0 deletions src/rt/memory_region.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ memory_region::realloc(void *mem, size_t orig_size) {

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", size);
abort();
}

# if RUSTRT_TRACK_ALLOCATIONS >= 1
assert(newMem->magic == MAGIC);
Expand Down Expand Up @@ -108,6 +112,10 @@ memory_region::malloc(size_t size, const char *tag, bool zero) {
size_t old_size = size;
size += HEADER_SIZE;
alloc_header *mem = (alloc_header *)::malloc(size);
if (mem == NULL) {
fprintf(stderr, "memory_region::malloc> Out of memory allocating %ld bytes", size);
abort();
}

# if RUSTRT_TRACK_ALLOCATIONS >= 1
mem->magic = MAGIC;
Expand Down
5 changes: 5 additions & 0 deletions src/rt/memory_region.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class memory_region {
void release_alloc(void *mem);
void claim_alloc(void *mem);

private:
// private and undefined to disable copying
memory_region(const memory_region& rhs);
memory_region& operator=(const memory_region& rhs);

public:
memory_region(rust_env *env, bool synchronized);
memory_region(memory_region *parent);
Expand Down
5 changes: 5 additions & 0 deletions src/rt/rust_sched_launcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class rust_sched_launcher : public kernel_owned<rust_sched_launcher> {
private:
rust_sched_loop sched_loop;

private:
// private and undefined to disable copying
rust_sched_launcher(const rust_sched_launcher& rhs);
rust_sched_launcher& operator=(const rust_sched_launcher& rhs);

protected:
rust_sched_driver driver;

Expand Down
5 changes: 5 additions & 0 deletions src/rt/rust_sched_loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ struct rust_sched_loop

void pump_loop();

private:
// private and undefined to disable copying
rust_sched_loop(const rust_sched_loop& rhs);
rust_sched_loop& operator=(const rust_sched_loop& rhs);

public:
rust_kernel *kernel;
rust_scheduler *sched;
Expand Down
5 changes: 5 additions & 0 deletions src/rt/rust_scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ class rust_scheduler : public kernel_owned<rust_scheduler> {
// Called when refcount reaches zero
void delete_this();

private:
// private and undefined to disable copying
rust_scheduler(const rust_scheduler& rhs);
rust_scheduler& operator=(const rust_scheduler& rhs);

public:
rust_scheduler(rust_kernel *kernel, size_t max_num_threads,
rust_sched_id id, bool allow_exit, bool killed,
Expand Down
6 changes: 6 additions & 0 deletions src/rt/rust_signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ class rust_signal {
public:
virtual void signal() = 0;
virtual ~rust_signal() {}
rust_signal() {}

private:
// private and undefined to disable copying
rust_signal(const rust_signal& rhs);
rust_signal& operator=(const rust_signal& rhs);
};

#endif /* RUST_SIGNAL_H */
5 changes: 5 additions & 0 deletions src/rt/rust_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ rust_task : public kernel_owned<rust_task>
void wakeup_inner(rust_cond *from);
bool blocked_on(rust_cond *cond);

private:
// private and undefined to disable copying
rust_task(const rust_task& rhs);
rust_task& operator=(const rust_task& rhs);

public:

// Only a pointer to 'name' is kept, so it must live as long as this task.
Expand Down
53 changes: 38 additions & 15 deletions src/rt/util/array_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,35 @@

#include <inttypes.h>
#include <stddef.h>
#include <new>

/**
* A simple, resizable array list.
* A simple, resizable array list. Note that this only works with POD types
* (because data is grown via realloc).
*/
template<typename T> class array_list {
static const size_t INITIAL_CAPACITY = 8;
size_t _size;
T * _data;
size_t _capacity;
private:
// private and left undefined to disable copying
array_list(const array_list& rhs);
array_list& operator=(const array_list& rhs);
public:
array_list();
~array_list();
size_t size();
size_t size() const;
int32_t append(T value);
int32_t push(T value);
bool pop(T *value);
void pop(T *value);
bool replace(T old_value, T new_value);
int32_t index_of(T value);
bool is_empty();
int32_t index_of(T value) const;
bool is_empty() const;
T* data();
const T* data() const;
T & operator[](size_t index);
const T & operator[](size_t index) const;
};

template<typename T>
Expand All @@ -40,7 +48,7 @@ array_list<T>::~array_list() {
}

template<typename T> size_t
array_list<T>::size() {
array_list<T>::size() const {
return _size;
}

Expand All @@ -52,24 +60,27 @@ array_list<T>::append(T value) {
template<typename T> int32_t
array_list<T>::push(T value) {
if (_size == _capacity) {
_capacity = _capacity * 2;
_data = (T *) realloc(_data, _capacity * sizeof(T));
size_t new_capacity = _capacity * 2;
void* buffer = realloc(_data, new_capacity * sizeof(T));
if (buffer == NULL) {
fprintf(stderr, "array_list::push> Out of memory allocating %ld bytes", new_capacity * sizeof(T));
abort();
}
_data = (T *) buffer;
_capacity = new_capacity;
}
_data[_size ++] = value;
return _size - 1;
}

template<typename T> bool
template<typename T> void
array_list<T>::pop(T *value) {
if (_size == 0) {
return false;
}
assert(_size > 0);
if (value != NULL) {
*value = _data[-- _size];
} else {
-- _size;
}
return true;
}

/**
Expand All @@ -87,7 +98,7 @@ array_list<T>::replace(T old_value, T new_value) {
}

template<typename T> int32_t
array_list<T>::index_of(T value) {
array_list<T>::index_of(T value) const {
for (size_t i = 0; i < _size; i++) {
if (_data[i] == value) {
return i;
Expand All @@ -98,11 +109,18 @@ array_list<T>::index_of(T value) {

template<typename T> T &
array_list<T>::operator[](size_t index) {
assert(index < size());
return _data[index];
}

template<typename T> const T &
array_list<T>::operator[](size_t index) const {
assert(index < size());
return _data[index];
}

template<typename T> bool
array_list<T>::is_empty() {
array_list<T>::is_empty() const {
return _size == 0;
}

Expand All @@ -111,4 +129,9 @@ array_list<T>::data() {
return _data;
}

template<typename T> const T*
array_list<T>::data() const {
return _data;
}

#endif /* ARRAY_LIST_H */
18 changes: 11 additions & 7 deletions src/rt/util/hash_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ template<typename K, typename V> class hash_map {
UT_hash_handle hh;
};
map_entry * _head;
private:
// private and left undefined to disable copying
hash_map(const hash_map& rhs);
hash_map& operator=(const hash_map& rhs);
public:
hash_map();
~hash_map();
Expand Down Expand Up @@ -54,7 +58,7 @@ template<typename K, typename V> class hash_map {
* true if the value was found and updates the specified *value parameter
* with the associated value, or false otherwise.
*/
bool get(K key, V *value);
bool get(K key, V *value) const;

/**
* Removes a key-value pair from this hash map.
Expand All @@ -71,7 +75,7 @@ template<typename K, typename V> class hash_map {
* returns:
* true if the specified key exists in this hash map, or false otherwise.
*/
bool contains(K key);
bool contains(K key) const;

/**
* Removes the value associated with the specified key from this hash map.
Expand All @@ -86,9 +90,9 @@ template<typename K, typename V> class hash_map {
/**
* Returns the number of key-value pairs in this hash map.
*/
size_t count();
size_t count() const;

bool is_empty() {
bool is_empty() const {
return count() == 0;
}

Expand Down Expand Up @@ -124,7 +128,7 @@ hash_map<K,V>::put(K key, V value) {
}

template<typename K, typename V> bool
hash_map<K,V>::get(K key, V *value) {
hash_map<K,V>::get(K key, V *value) const {
map_entry *entry = NULL;
HASH_FIND(hh, _head, &key, sizeof(K), entry);
if (entry == NULL) {
Expand All @@ -146,7 +150,7 @@ hash_map<K,V>::set(K key, V value) {
}

template<typename K, typename V> bool
hash_map<K,V>::contains(K key) {
hash_map<K,V>::contains(K key) const {
V value;
return get(key, &value);
}
Expand Down Expand Up @@ -184,7 +188,7 @@ hash_map<K,V>::remove(K key) {
}

template<typename K, typename V> size_t
hash_map<K,V>::count() {
hash_map<K,V>::count() const {
return HASH_CNT(hh, _head);
}

Expand Down
25 changes: 17 additions & 8 deletions src/rt/util/indexed_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

class indexed_list_object {
public:
virtual ~indexed_list_object() {}
int32_t list_index;
};

Expand All @@ -39,21 +40,22 @@ class indexed_list_element : public indexed_list_object {
template<typename T> class indexed_list {
array_list<T*> list;
public:
virtual int32_t append(T *value);
virtual bool pop(T **value);
int32_t append(T *value);
bool pop(T **value);
/**
* Same as pop(), except that it returns NULL if the list is empty.
*/
virtual T* pop_value();
virtual size_t length() {
T* pop_value();
size_t length() const {
return list.size();
}
virtual bool is_empty() {
bool is_empty() const {
return list.is_empty();
}
virtual int32_t remove(T* value);
virtual T * operator[](int32_t index);
virtual ~indexed_list() {}
int32_t remove(T* value);
T * operator[](int32_t index);
const T * operator[](int32_t index) const;
~indexed_list() {}
};

template<typename T> int32_t
Expand Down Expand Up @@ -104,4 +106,11 @@ indexed_list<T>::operator[](int32_t index) {
return value;
}

template <typename T> const T *
indexed_list<T>::operator[](int32_t index) const {
T *value = list[index];
assert(value->list_index == index);
return value;
}

#endif /* INDEXED_LIST_H */