mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Fixed dynamic memory allocation.
This commit is contained in:
parent
cd22973d48
commit
1fb2085c67
@ -52,7 +52,6 @@ static uint16_t self_statusmessage_len;
|
|||||||
static USERSTATUS self_userstatus;
|
static USERSTATUS self_userstatus;
|
||||||
|
|
||||||
static Friend *friendlist;
|
static Friend *friendlist;
|
||||||
static uint32_t numallocated;
|
|
||||||
static uint32_t numfriends;
|
static uint32_t numfriends;
|
||||||
|
|
||||||
|
|
||||||
@ -60,18 +59,13 @@ static uint32_t numfriends;
|
|||||||
0 if we are offline
|
0 if we are offline
|
||||||
static uint8_t online; */
|
static uint8_t online; */
|
||||||
|
|
||||||
/* double the size of the friend list
|
/* set the size of the friend list to numfriends
|
||||||
return -1 if realloc fails */
|
return -1 if realloc fails */
|
||||||
int realloc_friendlist(void) {
|
int realloc_friendlist(uint32_t num) {
|
||||||
if (numallocated == 0)
|
Friend *newfriendlist = realloc(friendlist, num*sizeof(Friend));
|
||||||
numallocated = 1; /* initial size */
|
|
||||||
else
|
|
||||||
numallocated *= 2; /* double each time */
|
|
||||||
|
|
||||||
Friend *newfriendlist = realloc(friendlist, numallocated*sizeof(Friend));
|
|
||||||
if (newfriendlist == NULL)
|
if (newfriendlist == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
|
memset(&newfriendlist[num-1], 0, sizeof(Friend));
|
||||||
friendlist = newfriendlist;
|
friendlist = newfriendlist;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -134,8 +128,7 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length)
|
|||||||
return FAERR_ALREADYSENT;
|
return FAERR_ALREADYSENT;
|
||||||
|
|
||||||
/* resize the friend list if necessary */
|
/* resize the friend list if necessary */
|
||||||
if (numfriends + 1 > numallocated)
|
realloc_friendlist(numfriends + 1);
|
||||||
realloc_friendlist();
|
|
||||||
|
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
for (i = 0; i <= numfriends; ++i) {
|
for (i = 0; i <= numfriends; ++i) {
|
||||||
@ -166,8 +159,7 @@ int m_addfriend_norequest(uint8_t * client_id)
|
|||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
/* resize the friend list if necessary */
|
/* resize the friend list if necessary */
|
||||||
if (numfriends + 1 > numallocated)
|
realloc_friendlist(numfriends + 1);
|
||||||
realloc_friendlist();
|
|
||||||
|
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
for (i = 0; i <= numfriends; ++i) {
|
for (i = 0; i <= numfriends; ++i) {
|
||||||
@ -208,6 +200,7 @@ int m_delfriend(int friendnumber)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
numfriends = i;
|
numfriends = i;
|
||||||
|
realloc_friendlist(numfriends + 1);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user