aboutsummaryrefslogtreecommitdiffstats
path: root/main/astmm.c
diff options
context:
space:
mode:
authorkpfleming <kpfleming@f38db490-d61c-443f-a65b-d21fe96a405b>2008-07-11 14:03:52 +0000
committerkpfleming <kpfleming@f38db490-d61c-443f-a65b-d21fe96a405b>2008-07-11 14:03:52 +0000
commit17a007be4c2f38d6d2a3d5ece077303def2f34c5 (patch)
tree798a91357a1d31367c2ce060224033f989fb4a71 /main/astmm.c
parente763144be4e10434025e406b6eec86c22339c808 (diff)
fix a flaw found while experimenting with structure alignment and padding; low-fence checking would not work properly on 64-bit platforms, because the compiler was putting 4 bytes of padding between the fence field and the allocation memory block
added a very obvious runtime warning if this condition reoccurs, so the developer who broke it can be chastised into fixing it :-) git-svn-id: http://svn.digium.com/svn/asterisk/branches/1.4@129966 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'main/astmm.c')
-rw-r--r--main/astmm.c20
1 files changed, 19 insertions, 1 deletions
diff --git a/main/astmm.c b/main/astmm.c
index eeeaf5bd8..bc2cd824a 100644
--- a/main/astmm.c
+++ b/main/astmm.c
@@ -31,6 +31,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include <stdio.h>
#include <string.h>
+#include <stddef.h>
#include <time.h>
#include "asterisk/cli.h"
@@ -66,14 +67,26 @@ enum func_type {
static FILE *mmlog;
+/* NOTE: Be EXTREMELY careful with modifying this structure; the total size of this structure
+ must result in 'automatic' alignment so that the 'fence' field lands exactly at the end of
+ the structure in memory (and thus immediately before the allocated region the fence is
+ supposed to be used to monitor). In other words, we cannot allow the compiler to insert
+ any padding between this structure and anything following it, so add up the sizes of all the
+ fields and compare to sizeof(struct ast_region)... if they don't match, then the compiler
+ is padding the structure and either the fields need to be rearranged to eliminate internal
+ padding, or a dummy field will need to be inserted before the 'fence' field to push it to
+ the end of the actual space it will consume. Note that this must be checked for both 32-bit
+ and 64-bit platforms, as the sizes of pointers and 'size_t' differ on these platforms.
+*/
+
static struct ast_region {
struct ast_region *next;
+ size_t len;
char file[40];
char func[40];
unsigned int lineno;
enum func_type which;
unsigned int cache; /* region was allocated as part of a cache pool */
- size_t len;
unsigned int fence;
unsigned char data[0];
} *regions[SOME_PRIME];
@@ -463,6 +476,11 @@ static struct ast_cli_entry cli_memory[] = {
void __ast_mm_init(void)
{
char filename[PATH_MAX];
+ int pad;
+
+ if ((pad = (__alignof__(struct ast_region) - (offsetof(struct ast_region, data) % __alignof__(struct ast_region)))) != 0) {
+ ast_log(LOG_ERROR, "struct ast_region has %d bytes of padding! This must be eliminated for low-fence checking to work properly!\n", pad);
+ }
ast_cli_register_multiple(cli_memory, sizeof(cli_memory) / sizeof(struct ast_cli_entry));