Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
LSH
lsh
Commits
db36a82d
Commit
db36a82d
authored
Sep 22, 1998
by
Niels Möller
Browse files
Improved sanity checks.
Rev: src/xalloc.c:1.6 Rev: src/xalloc.h:1.5
parent
07e8b766
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/xalloc.c
View file @
db36a82d
...
...
@@ -28,13 +28,14 @@
#ifdef DEBUG_ALLOC
void
*
debug_malloc
(
size_t
size
)
void
*
debug_malloc
(
size_t
real_
size
)
{
static
int
count
=
4711
;
int
*
res
;
int
size
;
/* Count size in ints, and round up */
size
=
(
size
+
sizeof
(
int
)
-
1
)
/
sizeof
(
int
);
size
=
(
real_
size
+
sizeof
(
int
)
-
1
)
/
sizeof
(
int
);
res
=
malloc
((
size
+
3
)
*
sizeof
(
int
));
...
...
@@ -43,23 +44,49 @@ void *debug_malloc(size_t size)
res
[
0
]
=
count
;
res
[
1
]
=
size
;
((
struct
lsh_object
*
)
(
res
+
2
))
->
type
=
real_size
;
res
[
size
+
2
]
=
~
count
;
count
++
;
return
(
void
*
)
(
res
+
2
);
}
void
debug_
free
(
void
*
m
)
void
debug_
check_object
(
void
*
m
,
UINT32
expected_size
)
{
int
*
p
=
(
int
*
)
m
;
size_t
size
=
p
[
-
1
];
int
real_size
=
((
struct
lsh_object
*
)
m
)
->
type
;
if
(
real_size
)
{
/* Heap allocated object */
int
*
p
=
(
int
*
)
m
;
size_t
size
=
p
[
-
1
];
if
(
~
p
[
-
2
]
!=
p
[
size
])
fatal
(
"Memory currupted!
\n
"
);
if
(
expected_size
>
real_size
)
fatal
(
"Type error: pointing at too small an object!
\n
"
);
if
(
~
p
[
-
2
]
!=
p
[
size
])
fatal
(
"Memory corrupted!
\n
"
);
if
(
expected_size
>
size
*
sizeof
(
int
))
fatal
(
"Memory corrupted!
\n
"
);
}
}
p
[
-
2
]
=
p
[
size
]
=
0
;
free
(
p
-
2
);
void
debug_free
(
void
*
m
)
{
int
real_size
=
((
struct
lsh_object
*
)
m
)
->
type
;
if
(
real_size
)
{
/* Heap allocated object */
int
*
p
=
(
int
*
)
m
;
size_t
size
=
p
[
-
1
];
if
(
~
p
[
-
2
]
!=
p
[
size
])
fatal
(
"Memory corrupted!
\n
"
);
p
[
-
2
]
=
p
[
size
]
=
0
;
free
(
p
-
2
);
}
else
fatal
(
"Freeing an object not allcoated on the heap!
\n
"
);
}
#endif
...
...
@@ -77,6 +104,7 @@ struct lsh_string *lsh_string_alloc(UINT32 length)
struct
lsh_string
*
packet
=
xalloc
(
sizeof
(
struct
lsh_string
)
-
1
+
length
);
packet
->
length
=
length
;
packet
->
sequence_number
=
0
;
return
packet
;
}
...
...
src/xalloc.h
View file @
db36a82d
...
...
@@ -50,14 +50,20 @@ struct lsh_string *lsh_string_alloc(UINT32 size);
void
lsh_string_free
(
struct
lsh_string
*
packet
);
#ifdef DEBUG_ALLOC
void
*
debug_malloc
(
size_t
size
);
void
debug_free
(
void
*
m
);
void
debug_check_object
(
void
*
m
,
UINT32
size
);
#define lsh_free debug_free
#define lsh_malloc debug_malloc
#else
#define MDEBUG(x) debug_check_object(x, sizeof(*(x)))
#else
/* !DEBUG_ALLOC */
#define lsh_free free
#define lsh_malloc malloc
#endif
#define MDEBUG(x)
#endif
/* !DEBUG_ALLOC */
#endif
/* LSH_XALLOC_H_INCLUDED */
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment