From: Pekka J Enberg Date: Sat, 17 Sep 2005 02:28:11 +0000 (-0700) Subject: [PATCH] CodingStyle: memory allocation X-Git-Tag: daily-2014.03.25.0_l4t/l4t-r19.1~50463^2~365 X-Git-Url: https://nv-tegra.nvidia.com/r/gitweb?p=linux-3.10.git;a=commitdiff_plain;h=af4e5a218e18ad588d60a4f9d6f8fb5db1a32587;ds=sidebyside [PATCH] CodingStyle: memory allocation This patch adds a new chapter on memory allocation to Documentation/CodingStyle. Signed-off-by: Pekka Enberg Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds --- diff --git a/Documentation/CodingStyle b/Documentation/CodingStyle index 22e5f9036f3..eb7db3c1922 100644 --- a/Documentation/CodingStyle +++ b/Documentation/CodingStyle @@ -410,7 +410,26 @@ Kernel messages do not have to be terminated with a period. Printing numbers in parentheses (%d) adds no value and should be avoided. - Chapter 13: References + Chapter 13: Allocating memory + +The kernel provides the following general purpose memory allocators: +kmalloc(), kzalloc(), kcalloc(), and vmalloc(). Please refer to the API +documentation for further information about them. + +The preferred form for passing a size of a struct is the following: + + p = kmalloc(sizeof(*p), ...); + +The alternative form where struct name is spelled out hurts readability and +introduces an opportunity for a bug when the pointer variable type is changed +but the corresponding sizeof that is passed to a memory allocator is not. + +Casting the return value which is a void pointer is redundant. The conversion +from void pointer to any other pointer type is guaranteed by the C programming +language. + + + Chapter 14: References The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie.