allocation

How do I use the Rust memory allocator for a C library that can be provided an allocator?

萝らか妹 提交于 2019-12-04 03:24:31
问题 I'm writing Rust bindings to a C library which has the option to use a third-party memory allocator. Its interface looks like this: struct allocator { void*(*alloc)(void *old, uint); void(*free)(void*); }; The corresponding Rust struct is, I guess, the following: #[repr(C)] #[derive(Copy, Clone, Debug, PartialEq)] pub struct Allocator { alloc: Option<extern "C" fn(*mut c_void, c_uint) -> *mut c_void>, free: Option<extern "C" fn(*mut c_void)>, } How can I implement these two extern functions

Calculate vector whose length is not known beforehand - should I “grow” it?

女生的网名这么多〃 提交于 2019-12-04 01:16:36
I need to calculate entries of a vector whose length I do not know beforehand . How to do so efficiently? A trivial solution is to "grow" it: start with a small or empty vector and successively append new entries until the stopping criterion is reached. For example: foo <- numeric(0) while ( sum(foo) < 100 ) foo <- c(foo,runif(1)) length(foo) # 195 However, "growing" vectors is frowned upon in R for performance reasons. Of course, I could "grow it in chunks": pre-allocate a "good-sized" vector, fill it, double its length when it is full, and finally cut it down to size. But this feels error

If a function returns an UnsafeMutablePointer is it our responsibility to destroy and dealloc?

耗尽温柔 提交于 2019-12-03 15:41:27
问题 For example if I were to write this code: var t = time_t() time(&t) let x = localtime(&t) // returns UnsafeMutablePointer<tm> println("\(x.memory.tm_hour): \(x.memory.tm_min): \(x.memory.tm_sec)") ...would it also be necessary to also do the following? x.destroy() x.dealloc(1) Or did we not allocate the memory and so therefore don't need to dismiss it? Update #1: If we imagine a function that returns an UnsafeMutablePointer : func point() -> UnsafeMutablePointer<String> { let a =

Dynamically allocate C struct?

倾然丶 夕夏残阳落幕 提交于 2019-12-03 13:24:18
I want to dynamically allocate a C struct: typedef struct { short *offset; char *values; } swc; Both 'offset' and 'values' are supposed to be arrays, but their size is unknown until runtime. How can I dynamically allocate memory for my struct and the struct's arrays? swc *a = (swc*)malloc(sizeof(swc)); a->offset = (short*)malloc(sizeof(short)*n); a->values = (char*)malloc(sizeof(char)*n); Where n = the number of items in each array and a is the address of the newly allocated data structure. Don't forget to free() offsets and values before free()'ing a. In C: swc *s = malloc(sizeof *s); //

Placing Python objects in shared memory

﹥>﹥吖頭↗ 提交于 2019-12-03 12:45:24
问题 Is there a Python module that would enable me to place instances of non-trivial user classes into shared memory? By that I mean allocating directly in shared memory as opposed to pickling into and out of it. multiprocessing.Value and multiprocessing.Array wouldn't work for my use case as they only seem to support primitive types and arrays thereof. The only thing I've found so far is POSH, but it hasn't changed in eight years. This suggests that it's either super-stable or is out of date.

Android: Track number of objects created

两盒软妹~` 提交于 2019-12-03 10:36:34
I'm porting a game to Android (there's a lot of code and very little of it is mine), and DalvikVM is telling me (through LogCat) all about the garbage collection. At some point in the code, I get a stream of "GC freed x objects / x ms" messages, basically informing me that ~150,000 objects have just been deleted and it's taking a full second. I want to know where these came from! I am pretty sure I'm not creating that many objects intentionally. So, is there a way to get... basically the opposite of that message? Something that prints a log message when any object is created ? That way I could

Why use [ClassName alloc] instead of [[self class] alloc]?

拥有回忆 提交于 2019-12-03 10:10:33
I'm reading through Mark Dalrymple's Learn Objective-C on the Mac (only at the chapter on Protocols, so still relatively newbish) and trying to figure something out: Why would you ever reference a class by its own name? If I had a class called Foo , why would I ever want to write, say, [[Foo alloc] init] and not [[[self class] alloc] init] If I had a subclass Bar, wouldn't the first option invalidate me from writing [[Bar alloc] init] whereas the second option would allow it? When would the first option be better? Generally, within a class method, you do use [[self alloc] init] . For example,

Why doesn't this C++ STL allocator allocate?

风格不统一 提交于 2019-12-03 09:21:28
问题 I'm trying to write a custom STL allocator that is derived from std::allocator , but somehow all calls to allocate() go to the base class. I have narrowed it down to this code: template <typename T> class a : public std::allocator<T> { public: T* allocate(size_t n, const void* hint = 0) const { cout << "yo!"; return 0; } }; int main() { vector<int, a<int>> v(1000, 42); return 0; } I expect "Yo!" to get printed, followed by some horrible error because I don't actually allocate anything.

Python function slows down with presence of large list

纵然是瞬间 提交于 2019-12-03 08:30:18
I was testing the speeds of a few different ways to do complex iterations over some of my data, and I found something weird. It seems that having a large list local to some function slows down that function considerably, even if it is not touching that list. For example, creating 2 independent lists via 2 instances of the same generator function is about 2.5x slower the second time. If the first list is removed prior to creating the second, both iterators go at the same spee. def f(): l1, l2 = [], [] for c1, c2 in generatorFxn(): l1.append((c1, c2)) # destroying l1 here fixes the problem for

HDF5 struct with pointer array

那年仲夏 提交于 2019-12-03 07:38:49
I am trying to write a HDF5 file with a structure which contains an int and a float* typedef struct s1_t { int a; float *b; } s1_t; However, upon allocating the float* and putting values into it, I still can't output the data in my hdf5 file. I believe this is because the write function assumes that the compound data type is contiguous when a dynamically allocated array will not be. Is there any way around this problem by still using a pointer array? /* * This example shows how to create a compound data type with an array member, * and write an array which has the compound data type to the