diff options
author | irungentoo <irungentoo@gmail.com> | 2013-08-08 08:50:06 -0700 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-08-08 08:50:06 -0700 |
commit | cd22973d48102a6b90dbcb46e58616f7decc1d59 (patch) | |
tree | 9fae866e929c6f5aff515670e9b4ea677d9c8e78 | |
parent | a02390836e102eaecc123348a6ffcedb082198b9 (diff) | |
parent | 815628b76af19f8ed01a77057b61bd1f9fb9c5b7 (diff) |
Merge pull request #396 from lukechampine/dynamicfriends
make friendlist resize dynamically
-rw-r--r-- | core/Messenger.c | 38 |
1 files changed, 31 insertions, 7 deletions
diff --git a/core/Messenger.c b/core/Messenger.c index 3d991e63..1df49d2a 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -51,16 +51,31 @@ static uint8_t *self_statusmessage; | |||
51 | static uint16_t self_statusmessage_len; | 51 | static uint16_t self_statusmessage_len; |
52 | static USERSTATUS self_userstatus; | 52 | static USERSTATUS self_userstatus; |
53 | 53 | ||
54 | #define MAX_NUM_FRIENDS 256 | 54 | static Friend *friendlist; |
55 | 55 | static uint32_t numallocated; | |
56 | static Friend friendlist[MAX_NUM_FRIENDS]; | ||
57 | |||
58 | static uint32_t numfriends; | 56 | static uint32_t numfriends; |
59 | 57 | ||
58 | |||
60 | /* 1 if we are online | 59 | /* 1 if we are online |
61 | 0 if we are offline | 60 | 0 if we are offline |
62 | static uint8_t online; */ | 61 | static uint8_t online; */ |
63 | 62 | ||
63 | /* double the size of the friend list | ||
64 | return -1 if realloc fails */ | ||
65 | int realloc_friendlist(void) { | ||
66 | if (numallocated == 0) | ||
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) | ||
73 | return -1; | ||
74 | |||
75 | friendlist = newfriendlist; | ||
76 | return 0; | ||
77 | } | ||
78 | |||
64 | /* return the friend id associated to that public key. | 79 | /* return the friend id associated to that public key. |
65 | return -1 if no such friend */ | 80 | return -1 if no such friend */ |
66 | int getfriend_id(uint8_t *client_id) | 81 | int getfriend_id(uint8_t *client_id) |
@@ -118,8 +133,12 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) | |||
118 | if (getfriend_id(client_id) != -1) | 133 | if (getfriend_id(client_id) != -1) |
119 | return FAERR_ALREADYSENT; | 134 | return FAERR_ALREADYSENT; |
120 | 135 | ||
136 | /* resize the friend list if necessary */ | ||
137 | if (numfriends + 1 > numallocated) | ||
138 | realloc_friendlist(); | ||
139 | |||
121 | uint32_t i; | 140 | uint32_t i; |
122 | for (i = 0; i <= numfriends && i <= MAX_NUM_FRIENDS; ++i) { /*TODO: dynamic memory allocation to allow for more than MAX_NUM_FRIENDS friends */ | 141 | for (i = 0; i <= numfriends; ++i) { |
123 | if (friendlist[i].status == NOFRIEND) { | 142 | if (friendlist[i].status == NOFRIEND) { |
124 | DHT_addfriend(client_id); | 143 | DHT_addfriend(client_id); |
125 | friendlist[i].status = FRIEND_ADDED; | 144 | friendlist[i].status = FRIEND_ADDED; |
@@ -145,8 +164,13 @@ int m_addfriend_norequest(uint8_t * client_id) | |||
145 | { | 164 | { |
146 | if (getfriend_id(client_id) != -1) | 165 | if (getfriend_id(client_id) != -1) |
147 | return -1; | 166 | return -1; |
167 | |||
168 | /* resize the friend list if necessary */ | ||
169 | if (numfriends + 1 > numallocated) | ||
170 | realloc_friendlist(); | ||
171 | |||
148 | uint32_t i; | 172 | uint32_t i; |
149 | for (i = 0; i <= numfriends && i <= MAX_NUM_FRIENDS; ++i) { /*TODO: dynamic memory allocation to allow for more than MAX_NUM_FRIENDS friends */ | 173 | for (i = 0; i <= numfriends; ++i) { |
150 | if(friendlist[i].status == NOFRIEND) { | 174 | if(friendlist[i].status == NOFRIEND) { |
151 | DHT_addfriend(client_id); | 175 | DHT_addfriend(client_id); |
152 | friendlist[i].status = FRIEND_REQUESTED; | 176 | friendlist[i].status = FRIEND_REQUESTED; |
@@ -158,7 +182,7 @@ int m_addfriend_norequest(uint8_t * client_id) | |||
158 | friendlist[i].userstatus = USERSTATUS_NONE; | 182 | friendlist[i].userstatus = USERSTATUS_NONE; |
159 | friendlist[i].message_id = 0; | 183 | friendlist[i].message_id = 0; |
160 | friendlist[i].receives_read_receipts = 1; /* default: YES */ | 184 | friendlist[i].receives_read_receipts = 1; /* default: YES */ |
161 | numfriends++; | 185 | ++numfriends; |
162 | return i; | 186 | return i; |
163 | } | 187 | } |
164 | } | 188 | } |