Reputation: 97
I am aware that an implementer has a choice of whether he wants to zero a malloc page or let OS give him a zeroed page (for more optimization purposes).
My question is simple - in Ubuntu 14.04 LTS which comes with linux kernel 3.16 and gcc 4.8.4, who will zero my pages? Is it in user land or kernel land?
Upvotes: 2
Views: 129
Reputation: 36607
That depends on the implementer of your standard library, not on the host system. It is not possible to give a specific answer for a particular OS, since it may be the build target of multiple compilers and their libraries - including on other systems, if you consider the possibility of cross-compiling (building on one type of system to target another).
Most implementations I've seen of calloc()
use a call of malloc()
followed by either a call of memset()
or (with some implementations that target unix) a legacy function called bzero()
- which is, itself, sometimes replaced by a macro call that expands to a call of memset()
in a number of recent versions of libraries.
memset()
is often hand-optimised. But, again, it is up to the implementer of the library.
Upvotes: 1
Reputation: 772
It can depend on where the memory came from. The calloc
code is userland, and will zero a memory page that gets re-used by a process. This happens when the memory is previously used and then freed, but not returned to the OS. However, if the page is newly allocated to the process, it will come already cleared to 0 by the OS (for security purposes), and so does not need to be cleared by calloc
. This means calloc
can potentially be faster than calling malloc
followed by memset
, since it can skip the memset
if it knows it will already by zeroed.
Upvotes: 2