Debugging Your Operating System: A Lesson in Memory Allocation
lukasa.co.uk
lukasa.co.uk
void *calloc(size_t count, size_t size) {
assert(!multiplication_would_overflow(count, size));
size_t allocation_size = count * size;
void *allocation = malloc(allocation_size);
memset(allocation, 0, allocation_size);
return allocation;
}
Should really return (void *)0 on overflow, instead of asserting.Edit: apologies for being somewhat ambiguous. The "bug" I refer to is the Radar issue the writer of the blog created, not the performance issue with the Python code.
I've tracked down corner cases like this myself and been correctly told by developers that they're not worth fixing. But you don't know that a priori.
Incidentally, the blog post asks "why was the memory being actively zeroed at all?" but never actually answers it.
Is that common?
Its definitely interesting to see these very low-level implementation details bubble up to the surface from time to time.
In this case, both Windows and Linux are used for huge server farms. MacOS not so much. Apple probably doesn't spend the energy optimizing this sort of thing since it's less likely to cause trouble for them.
One could perhaps pass in a bytearray object, or even have it return the same bytearray object each loop, with a very large warning on the tin about how you will want to take a copy if you want to keep it outside the loop's body. (i.e., the returned bytearray is owned by the iterator, and you're merely borrowing it for the loop; this is a potentially confusing API but would reduce the memory allocation churn significantly: you'd only alloc your buffer once, instead of O(download size) times.)
This again? Just 4 days ago there was an article making up the same nonsense.
Seems perfectly reasonable. Care to enlighten me?
If I had to hazard a guess, it felt like the first one was potentially written by the reporter of the initial bug, the second by the actual developer who investigated it. This is just a guess, but I bet if you reread both you'll likely agree with me.
Do you have some evidence to counter?
I'm genuinely curious here.
I specifically asked for "historical evidence" because I expected that the first thing on your (or someone else's) mind would be to make this inference from prototype to intent. This expectation was met, and unfortunately you disregarded this subtlelity and moved on to reply. You didn't go look for historical evidence (that does not exist), a path which could have led to the "enlightenment" you sought. Historical claims should be based on historical evidence, not made up.
Just because an something is so, does not mean that it was intended to be so for reason X. As you'll find with experience, rationale cannot be deduced from code. Code is merely an indicator.
I don't mind putting in work to answer questions, but I won't put in busy work. If there's someone in front of me that knows the answers, I'd rather ask that person than try to find those answers again.
>a path which could have led to the "enlightenment" you sought.
But I wasn't seeking "enlightenment," or anything of the sort. I was seeking a piece of knowledge that you posessed. You essentially told me to go find it myself. I'm not doing that, because my time is a finite resource, and the person I'm talking to already knows and could tell me.