Bak ?ls -s 0 in

Age:26 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: Thu Jun 17, 2004 3:16 pm Post subject: Where should pointers used in arenadata be initialized? |
 |
|
|
|
I'm looking at balls.c and how it handles per arena data allocation:
abdkey = aman->AllocateArenaData(sizeof(ArenaBallData));
so it gets a key for the data... now I'd assume any pointers in the data (like BallData *balls) would now contain a random value until i amalloc or malloc something for it. So my question is, where does this value get initialized?
The obvious answer is when the arena is created, so I looked in the AABall function:
void AABall(Arena *arena, int action)
{
/* create the mutex if necessary */
if (action == AA_CREATE)
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init((pthread_mutex_t*)P_ARENA_DATA(arena, mtxkey), &attr);
pthread_mutexattr_destroy(&attr);
}
LOCK_STATUS(arena);
if (action == AA_CREATE || action == AA_DESTROY)
{
ArenaBallData *abd = P_ARENA_DATA(arena, abdkey);
/* clean up old ball data */
if (abd->balls)
{
afree(abd->balls);
abd->balls = NULL;
}
abd->ballcount = 0;
}
if (action == AA_CREATE)
{
/* only if we're creating, load the data */
load_ball_settings(arena, 1);
}
....
it looks like the first thing it does if action == AA_CREATE is create some thread stuff... then it does if (action == AA_CREATE || action == AA_DESTROY)
and if (abd->balls)
then it'll free the memory pointed to by abd->balls. But if abd->balls is a random value(just been allocated) wouldn't it be trying to free a random memory location? It doesn't. I put a check in and when the arena is created abd->balls is zero so it skips that if statement. But where does abd become zero??? when you do allocatearenadata does it initialize all the data to zero?
Also: when should I do LOCK_STATUS(arena); and UNLOCK_STATUS(arena); ??? is that whenever I get or modify arena data? what about when I get player data? LOCK_PLAYER?
Thanks!!! |
|