Hi Johan, On Wed, Feb 23, 2011 at 10:33 PM, Claudio Takahasi <claudio.takahasi@xxxxxxxxxxxxx> wrote: > Use glib memory allocation functions instead of malloc for attribute > data list in ATT protocol utility functions. > --- > Âattrib/att.c | Â101 ++++++++++++++++++++++++++++++++++++++++++---------------- > Âattrib/att.h | Â Â1 + > Â2 files changed, 74 insertions(+), 28 deletions(-) > > diff --git a/attrib/att.c b/attrib/att.c > index 3259fca..1de93c4 100644 > --- a/attrib/att.c > +++ b/attrib/att.c > @@ -80,13 +80,47 @@ const char *att_ecode2str(uint8_t status) > > Âvoid att_data_list_free(struct att_data_list *list) > Â{ > + Â Â Â if (list == NULL) > + Â Â Â Â Â Â Â return; > + > + Â Â Â if (list->data) { > + Â Â Â Â Â Â Â int i; > + Â Â Â Â Â Â Â for (i = 0; i < list->num; i++) > + Â Â Â Â Â Â Â Â Â Â Â g_free(list->data[i]); > + Â Â Â } > + > + Â Â Â g_free(list->data); > + Â Â Â g_free(list); > +} > + > +struct att_data_list *att_data_list_alloc(uint16_t num, uint16_t len) > +{ > + Â Â Â struct att_data_list *list; > Â Â Â Âint i; > > - Â Â Â for (i = 0; i < list->num; i++) > - Â Â Â Â Â Â Â free(list->data[i]); > + Â Â Â list = g_try_new0(struct att_data_list, 1); > + Â Â Â if (list == NULL) > + Â Â Â Â Â Â Â return NULL; > + > + Â Â Â list->len = len; > + Â Â Â list->num = num; > + > + Â Â Â list->data = g_try_malloc0(sizeof(uint8_t *) * num); > + Â Â Â if (list->data == NULL) > + Â Â Â Â Â Â Â goto enomem; > > - Â Â Â free(list->data); > - Â Â Â free(list); > + Â Â Â for (i = 0; i < num; i++) { > + Â Â Â Â Â Â Â list->data[i] = g_try_malloc0(sizeof(uint8_t) * len); > + Â Â Â Â Â Â Â if (list->data[i] == NULL) > + Â Â Â Â Â Â Â Â Â Â Â goto enomem; > + Â Â Â } > + > + Â Â Â return list; > + > +enomem: > + Â Â Â att_data_list_free(list); > + > + Â Â Â return NULL; > Â} > > Âuint16_t enc_read_by_grp_req(uint16_t start, uint16_t end, uuid_t *uuid, > @@ -178,20 +212,21 @@ struct att_data_list *dec_read_by_grp_resp(const uint8_t *pdu, int len) > Â{ > Â Â Â Âstruct att_data_list *list; > Â Â Â Âconst uint8_t *ptr; > + Â Â Â uint16_t elen, num; > Â Â Â Âint i; > > Â Â Â Âif (pdu[0] != ATT_OP_READ_BY_GROUP_RESP) > Â Â Â Â Â Â Â Âreturn NULL; > > - Â Â Â list = malloc(sizeof(struct att_data_list)); > - Â Â Â list->len = pdu[1]; > - Â Â Â list->num = (len - 2) / list->len; > + Â Â Â elen = pdu[1]; > + Â Â Â num = (len - 2) / elen; > + Â Â Â list = att_data_list_alloc(num, elen); > + Â Â Â if (list == NULL) > + Â Â Â Â Â Â Â return NULL; > > - Â Â Â list->data = malloc(sizeof(uint8_t *) * list->num); > Â Â Â Âptr = &pdu[2]; > > - Â Â Â for (i = 0; i < list->num; i++) { > - Â Â Â Â Â Â Â list->data[i] = malloc(sizeof(uint8_t) * list->len); > + Â Â Â for (i = 0; i < num; i++) { > Â Â Â Â Â Â Â Âmemcpy(list->data[i], ptr, list->len); > Â Â Â Â Â Â Â Âptr += list->len; > Â Â Â Â} > @@ -307,7 +342,10 @@ GSList *dec_find_by_type_resp(const uint8_t *pdu, int len) > Â Â Â Â Â Â Â Âreturn NULL; > > Â Â Â Âfor (offset = 1, matches = NULL; len >= (offset + 4); offset += 4) { > - Â Â Â Â Â Â Â range = malloc(sizeof(struct att_range)); > + Â Â Â Â Â Â Â range = g_try_new0(struct att_range, 1); > + Â Â Â Â Â Â Â if (range == NULL) > + Â Â Â Â Â Â Â Â Â Â Â goto enomem; > + > Â Â Â Â Â Â Â Ârange->start = att_get_u16(&pdu[offset]); > Â Â Â Â Â Â Â Ârange->end = att_get_u16(&pdu[offset + 2]); > > @@ -315,6 +353,12 @@ GSList *dec_find_by_type_resp(const uint8_t *pdu, int len) > Â Â Â Â} > > Â Â Â Âreturn matches; > + > +enomem: > + Â Â Â g_slist_foreach(matches, (GFunc) g_free, NULL); > + Â Â Â g_slist_free(matches); > + > + Â Â Â return NULL; > Â} > > Âuint16_t enc_read_by_type_req(uint16_t start, uint16_t end, uuid_t *uuid, > @@ -406,20 +450,21 @@ struct att_data_list *dec_read_by_type_resp(const uint8_t *pdu, int len) > Â{ > Â Â Â Âstruct att_data_list *list; > Â Â Â Âconst uint8_t *ptr; > + Â Â Â uint16_t elen, num; > Â Â Â Âint i; > > Â Â Â Âif (pdu[0] != ATT_OP_READ_BY_TYPE_RESP) > Â Â Â Â Â Â Â Âreturn NULL; > > - Â Â Â list = malloc(sizeof(struct att_data_list)); > - Â Â Â list->len = pdu[1]; > - Â Â Â list->num = (len - 2) / list->len; > + Â Â Â elen = pdu[1]; > + Â Â Â num = (len - 2) / elen; > + Â Â Â list = att_data_list_alloc(num, elen); > + Â Â Â if (list == NULL) > + Â Â Â Â Â Â Â return NULL; > > - Â Â Â list->data = malloc(sizeof(uint8_t *) * list->num); > Â Â Â Âptr = &pdu[2]; > > - Â Â Â for (i = 0; i < list->num; i++) { > - Â Â Â Â Â Â Â list->data[i] = malloc(sizeof(uint8_t) * list->len); > + Â Â Â for (i = 0; i < num; i++) { > Â Â Â Â Â Â Â Âmemcpy(list->data[i], ptr, list->len); > Â Â Â Â Â Â Â Âptr += list->len; > Â Â Â Â} > @@ -775,6 +820,7 @@ struct att_data_list *dec_find_info_resp(const uint8_t *pdu, int len, > Â{ > Â Â Â Âstruct att_data_list *list; > Â Â Â Âuint8_t *ptr; > + Â Â Â uint16_t elen, num; > Â Â Â Âint i; > > Â Â Â Âif (pdu == NULL) > @@ -787,22 +833,21 @@ struct att_data_list *dec_find_info_resp(const uint8_t *pdu, int len, > Â Â Â Â Â Â Â Âreturn 0; > > Â Â Â Â*format = pdu[1]; > - > - Â Â Â list = malloc(sizeof(struct att_data_list)); > - > - Â Â Â list->len = sizeof(pdu[0]) + sizeof(*format); > + Â Â Â elen = sizeof(pdu[0]) + sizeof(*format); > Â Â Â Âif (*format == 0x01) > - Â Â Â Â Â Â Â list->len += 2; > + Â Â Â Â Â Â Â elen += 2; > Â Â Â Âelse if (*format == 0x02) > - Â Â Â Â Â Â Â list->len += 16; > + Â Â Â Â Â Â Â elen += 16; > > - Â Â Â list->num = (len - 2) / list->len; > - Â Â Â list->data = malloc(sizeof(uint8_t *) * list->num); > + Â Â Â num = (len - 2) / elen; > > Â Â Â Âptr = (void *) &pdu[2]; > > - Â Â Â for (i = 0; i < list->num; i++) { > - Â Â Â Â Â Â Â list->data[i] = malloc(list->len); > + Â Â Â list = att_data_list_alloc(num, elen); > + Â Â Â if (list == NULL) > + Â Â Â Â Â Â Â return NULL; > + > + Â Â Â for (i = 0; i < num; i++) { > Â Â Â Â Â Â Â Âmemcpy(list->data[i], ptr, list->len); > Â Â Â Â Â Â Â Âptr += list->len; > Â Â Â Â} > @@ -859,7 +904,7 @@ struct attribute *dec_indication(const uint8_t *pdu, int len) > Â Â Â Âif (len < min_len) > Â Â Â Â Â Â Â Âreturn NULL; > > - Â Â Â a = malloc(sizeof(struct attribute) + len - min_len); > + Â Â Â a = g_try_malloc0(sizeof(struct attribute) + len - min_len); > Â Â Â Âif (a == NULL) > Â Â Â Â Â Â Â Âreturn NULL; > > diff --git a/attrib/att.h b/attrib/att.h > index 7d9afeb..76072c0 100644 > --- a/attrib/att.h > +++ b/attrib/att.h > @@ -185,6 +185,7 @@ static inline void att_put_u32(uint32_t src, void *dst) > Â Â Â Âbt_put_unaligned(htobl(src), (uint32_t *) dst); > Â} > > +struct att_data_list *att_data_list_alloc(uint16_t num, uint16_t len); > Âvoid att_data_list_free(struct att_data_list *list); > > Âconst char *att_ecode2str(uint8_t status); > -- > 1.7.4.1 > > A new patch series will be sent replacing g_try_malloc0 and g_try_new0 by g_malloc0 and g_new0 respectively. Claudio. -- To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html