diff options
author | irungentoo <irungentoo@gmail.com> | 2013-08-08 12:13:29 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-08-08 12:13:29 -0400 |
commit | 1fb2085c6716dcedc0a3547bf78584bdf20a7b40 (patch) | |
tree | 64932e348e45c6fd9d47b82c94a8a809628173a1 | |
parent | cd22973d48102a6b90dbcb46e58616f7decc1d59 (diff) |
Fixed dynamic memory allocation.
-rw-r--r-- | core/Messenger.c | 21 |
1 files changed, 7 insertions, 14 deletions
diff --git a/core/Messenger.c b/core/Messenger.c index 1df49d2a..ede8f718 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -52,7 +52,6 @@ static uint16_t self_statusmessage_len; | |||
52 | static USERSTATUS self_userstatus; | 52 | static USERSTATUS self_userstatus; |
53 | 53 | ||
54 | static Friend *friendlist; | 54 | static Friend *friendlist; |
55 | static uint32_t numallocated; | ||
56 | static uint32_t numfriends; | 55 | static uint32_t numfriends; |
57 | 56 | ||
58 | 57 | ||
@@ -60,18 +59,13 @@ static uint32_t numfriends; | |||
60 | 0 if we are offline | 59 | 0 if we are offline |
61 | static uint8_t online; */ | 60 | static uint8_t online; */ |
62 | 61 | ||
63 | /* double the size of the friend list | 62 | /* set the size of the friend list to numfriends |
64 | return -1 if realloc fails */ | 63 | return -1 if realloc fails */ |
65 | int realloc_friendlist(void) { | 64 | int realloc_friendlist(uint32_t num) { |
66 | if (numallocated == 0) | 65 | Friend *newfriendlist = realloc(friendlist, num*sizeof(Friend)); |
67 | numallocated = 1; /* initial size */ | ||
68 | else | ||
69 | numallocated *= 2; /* double each time */ | ||
70 | |||
71 | Friend *newfriendlist = realloc(friendlist, numallocated*sizeof(Friend)); | ||
72 | if (newfriendlist == NULL) | 66 | if (newfriendlist == NULL) |
73 | return -1; | 67 | return -1; |
74 | 68 | memset(&newfriendlist[num-1], 0, sizeof(Friend)); | |
75 | friendlist = newfriendlist; | 69 | friendlist = newfriendlist; |
76 | return 0; | 70 | return 0; |
77 | } | 71 | } |
@@ -134,8 +128,7 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) | |||
134 | return FAERR_ALREADYSENT; | 128 | return FAERR_ALREADYSENT; |
135 | 129 | ||
136 | /* resize the friend list if necessary */ | 130 | /* resize the friend list if necessary */ |
137 | if (numfriends + 1 > numallocated) | 131 | realloc_friendlist(numfriends + 1); |
138 | realloc_friendlist(); | ||
139 | 132 | ||
140 | uint32_t i; | 133 | uint32_t i; |
141 | for (i = 0; i <= numfriends; ++i) { | 134 | for (i = 0; i <= numfriends; ++i) { |
@@ -166,8 +159,7 @@ int m_addfriend_norequest(uint8_t * client_id) | |||
166 | return -1; | 159 | return -1; |
167 | 160 | ||
168 | /* resize the friend list if necessary */ | 161 | /* resize the friend list if necessary */ |
169 | if (numfriends + 1 > numallocated) | 162 | realloc_friendlist(numfriends + 1); |
170 | realloc_friendlist(); | ||
171 | 163 | ||
172 | uint32_t i; | 164 | uint32_t i; |
173 | for (i = 0; i <= numfriends; ++i) { | 165 | for (i = 0; i <= numfriends; ++i) { |
@@ -208,6 +200,7 @@ int m_delfriend(int friendnumber) | |||
208 | break; | 200 | break; |
209 | } | 201 | } |
210 | numfriends = i; | 202 | numfriends = i; |
203 | realloc_friendlist(numfriends + 1); | ||
211 | 204 | ||
212 | return 0; | 205 | return 0; |
213 | } | 206 | } |