Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to partition and use heap memory allocated en masse with Rust?

I've read a lot on Rust lately but am still only beginning to oxidize. My brain retains most of its C/C++ reflexes so pardon me if this question is not relevant because of how things are done in Rust.

Is it generally possible (desirable?) to allocate a block of an arbitrary size on the heap and then map a data structure over it with bindings taking ownership of smaller chunks of memory?

Using C99, one may write this:

typedef unsigned char BYTE;

typedef struct {
    size_t size;
    BYTE payload[];
} flex;

// ...

flex * flex_new(size_t _size) {
    flex *f = malloc(sizeof(flex) + _size * sizeof(BYTE));
    f->size = _size;
    return f;
}

Flexible length arrays may prove useful e.g. when implementing memory pools with chunks of variable size or when allocating memory on the heap dedicated to a thread. The data structure which required size is only known at runtime is allocated in an 'atomic' fashion and its members packed contiguously.

I am wondering whether a similar Rust implementation is possible (without unsafe constructs) and if it is, what it looks like. Maybe the compiler may infer some information using lifetime specifiers in the struct definition?

like image 898
dummydev Avatar asked Nov 01 '22 12:11

dummydev


1 Answers

In terms of 'flexible length arrays,' you can use Vec::with_capacity to allocate more than your immediate need:

let mut vec: Vec<int> = Vec::with_capacity(10);

// The vector contains no items, even though it has capacity for more
assert_eq!(vec.len(), 0);

// These are all done without reallocating...
for i in range(0i, 10) {
    vec.push(i);
}

// ...but this may make the vector reallocate
vec.push(11);

For the more general case, TypedArena is what you'd want to use, though.

like image 168
Steve Klabnik Avatar answered Dec 14 '22 23:12

Steve Klabnik