void test_queue_new_free(void) { int i; Queue *queue; /* Create and destroy a queue */ queue = queue_new(); queue_free(queue); /* Add lots of values and then destroy */ queue = queue_new(); for (i=0; i<1000; ++i) { queue_push_head(queue, &variable1); } queue_free(queue); /* Test allocation when there is no free memory */ alloc_test_set_limit(0); queue = queue_new(); assert(queue == NULL); }
static bool handle_error_rsp(struct bt_att *att, uint8_t *pdu, ssize_t pdu_len, uint8_t *opcode) { const struct bt_att_pdu_error_rsp *rsp; struct att_send_op *op = att->pending_req; if (pdu_len != sizeof(*rsp)) { *opcode = 0; return false; } rsp = (void *) pdu; *opcode = rsp->opcode; /* Attempt to change security */ if (!change_security(att, rsp->ecode)) return false; util_debug(att->debug_callback, att->debug_data, "Retrying operation %p", op); att->pending_req = NULL; /* Push operation back to request queue */ return queue_push_head(att->req_queue, op); }
static bool set_and_store_gatt_req(struct bt_scpp *scpp, struct gatt_request *req, unsigned int id) { req->id = id; return queue_push_head(scpp->gatt_op, req); }
int get_moving_average(Moving_Average *self, int data_entry_) { int *data_entry; //puts("created variable"); data_entry = (int *) malloc(sizeof(int)); //puts("allocated memory"); *data_entry = data_entry_; //printf("Set to %d", *data_entry); queue_push_head(self->data, data_entry); //puts("push to head successful"); self->data_sum += data_entry_; self->cur_length += 1; if (self->cur_length > self->max_length) { int *popped = (int *)queue_pop_tail(self->data); self->data_sum -= *popped; free(popped); self->cur_length -= 1; } self->data_avg = self->data_sum / self->cur_length; if (self->cur_length < (self->max_length / 8)) { //printf("%d\n", data_entry_); return data_entry_; } else { //printf("%d\n", self->data_avg); return self->data_avg; } }
static void set_and_store_gatt_req(struct bt_bas *bas, struct gatt_request *req, unsigned int id) { req->id = id; queue_push_head(bas->gatt_op, req); }
guint g_attrib_send(GAttrib *attrib, guint id, const guint8 *pdu, guint16 len, GAttribResultFunc func, gpointer user_data, GDestroyNotify notify) { struct attrib_callbacks *cb = NULL; bt_att_response_func_t response_cb = NULL; bt_att_destroy_func_t destroy_cb = NULL; unsigned int pend_id; if (!attrib) return 0; if (!pdu || !len) return 0; if (func || notify) { cb = new0(struct attrib_callbacks, 1); if (!cb) return 0; cb->result_func = func; cb->user_data = user_data; cb->destroy_func = notify; cb->parent = attrib; queue_push_head(attrib->callbacks, cb); response_cb = attrib_callback_result; destroy_cb = attrib_callbacks_remove; }
void test_queue_is_empty(void) { Queue *queue; queue = queue_new(); assert(queue_is_empty(queue)); queue_push_head(queue, &variable1); assert(!queue_is_empty(queue)); queue_pop_head(queue); assert(queue_is_empty(queue)); queue_push_tail(queue, &variable1); assert(!queue_is_empty(queue)); queue_pop_tail(queue); assert(queue_is_empty(queue)); queue_free(queue); }
Queue *generate_queue(void) { Queue *queue; int i; queue = queue_new(); /* Add some values */ for (i=0; i<1000; ++i) { queue_push_head(queue, &variable1); queue_push_head(queue, &variable2); queue_push_head(queue, &variable3); queue_push_head(queue, &variable4); } return queue; }
static void discover_desc(struct bt_scpp *scpp, GAttrib *attrib, uint16_t start, uint16_t end, bt_uuid_t *uuid, gatt_cb_t func, gpointer user_data) { unsigned int id; id = gatt_discover_desc(attrib, start, end, uuid, func, user_data); queue_push_head(scpp->gatt_op, UINT_TO_PTR(id)); }
void test_queue_push_head(void) { Queue *queue; int i; queue = queue_new(); /* Add some values */ for (i=0; i<1000; ++i) { queue_push_head(queue, &variable1); queue_push_head(queue, &variable2); queue_push_head(queue, &variable3); queue_push_head(queue, &variable4); } assert(!queue_is_empty(queue)); /* Check values come out of the tail properly */ assert(queue_pop_tail(queue) == &variable1); assert(queue_pop_tail(queue) == &variable2); assert(queue_pop_tail(queue) == &variable3); assert(queue_pop_tail(queue) == &variable4); /* Check values come back out of the head properly */ assert(queue_pop_head(queue) == &variable4); assert(queue_pop_head(queue) == &variable3); assert(queue_pop_head(queue) == &variable2); assert(queue_pop_head(queue) == &variable1); queue_free(queue); /* Test behavior when running out of memory. */ queue = queue_new(); alloc_test_set_limit(0); assert(!queue_push_head(queue, &variable1)); queue_free(queue); }
static void write_char(struct bt_scpp *scan, GAttrib *attrib, uint16_t handle, const uint8_t *value, size_t vlen, GAttribResultFunc func, gpointer user_data) { unsigned int id; id = gatt_write_char(attrib, handle, value, vlen, func, user_data); queue_push_head(scan->gatt_op, UINT_TO_PTR(id)); }
static void discover_desc(struct bt_scpp *scpp, GAttrib *attrib, uint16_t start, uint16_t end, bt_uuid_t *uuid, gatt_cb_t func, gpointer user_data) { unsigned int id; id = gatt_discover_desc(attrib, start, end, uuid, func, user_data); if (queue_push_head(scpp->gatt_op, UINT_TO_PTR(id))) return; error("scpp: Could not discover descriptor"); g_attrib_cancel(attrib, id); }
static void write_char(struct bt_scpp *scan, GAttrib *attrib, uint16_t handle, const uint8_t *value, size_t vlen, GAttribResultFunc func, gpointer user_data) { unsigned int id; id = gatt_write_char(attrib, handle, value, vlen, func, user_data); if (queue_push_head(scan->gatt_op, UINT_TO_PTR(id))) return; error("scpp: Could not read char"); g_attrib_cancel(attrib, id); }
static bool add_default_agent(struct agent *agent) { if (queue_peek_head(default_agents) == agent) return true; queue_remove(default_agents, agent); if (!queue_push_head(default_agents, agent)) return false; DBG("Default agent set to %s %s", agent->owner, agent->path); adapter_foreach(set_io_cap, agent); return true; }
void queue_push_nth (Queue *queue, void *data, unsigned int n) { /* Precondition */ assert(queue != NULL); assert(n <= queue->length); if (n == 0) { queue_push_head (queue, data); } else if (n == queue->length - 1){ queue_push_tail (queue, data); } else { slist_insert(queue->head, data, n); queue->length ++; } /* Postcondicion */ assert((int) queue->length == slist_length(queue->head)); }
static void start_element( void *ud, const xmlChar *xname, const xmlChar **attr_names) { char *name = (char*)xname; parse_data_t *pd = (parse_data_t*)ud; union { int id; void * pid; } id; int ii; // Check to see if the first element found has been closed // If so, ignore any junk following it. if (pd->closed_top) return; for (ii = 0; ii < TAG_MAP_SZ; ii++) { if (strcmp(name, tag_map[ii].tag) == 0) { id.id = tag_map[ii].id; break; } } if (ii == TAG_MAP_SZ) { hb_error("Unrecognized start tag (%s)", name); return; } if (pd->value) { free(pd->value); pd->value = NULL; } queue_push_head(pd->tag_stack, id.pid); hb_value_type_t gtype = 0; hb_value_t *gval = NULL; hb_value_t *current = queue_peek_head(pd->stack); switch (id.id) { case P_PLIST: { // Ignore } break; case P_KEY: { if (pd->key) free(pd->key); pd->key = NULL; } break; case P_DICT: { gval = hb_dict_init(); queue_push_head(pd->stack, gval); } break; case P_ARRAY: { gval = hb_value_array_init(); queue_push_head(pd->stack, gval); } break; case P_INTEGER: { } break; case P_REAL: { } break; case P_STRING: { } break; case P_DATE: { } break; case P_TRUE: { } break; case P_FALSE: { } break; case P_DATA: { } break; } // Add the element to the current container if (gval) { // There's an element to add if (current == NULL) { pd->plist = gval; return; } gtype = hb_value_type(current); if (gtype == HB_VALUE_TYPE_ARRAY) { hb_value_array_append(current, gval); } else if (gtype == HB_VALUE_TYPE_DICT) { if (pd->key == NULL) { hb_error("No key for dictionary item"); hb_value_free(&gval); } else { hb_dict_set(current, pd->key, gval); } } else { hb_error("Invalid container type. This shouldn't happen"); } } }
struct gatt_db_attribute *gatt_db_insert_service(struct gatt_db *db, uint16_t handle, const bt_uuid_t *uuid, bool primary, uint16_t num_handles) { struct gatt_db_service *service, *after; after = NULL; if (!db || handle < 1) return NULL; if (num_handles < 1 || (handle + num_handles - 1) > UINT16_MAX) return NULL; service = find_insert_loc(db, handle, handle + num_handles - 1, &after); if (service) { const bt_uuid_t *type; bt_uuid_t value; if (primary) type = &primary_service_uuid; else type = &secondary_service_uuid; gatt_db_attribute_get_service_uuid(service->attributes[0], &value); /* Check if service match */ if (!bt_uuid_cmp(&service->attributes[0]->uuid, type) && !bt_uuid_cmp(&value, uuid) && service->num_handles == num_handles && service->attributes[0]->handle == handle) return service->attributes[0]; return NULL; } service = gatt_db_service_create(uuid, handle, primary, num_handles); if (!service) return NULL; if (after) { if (!queue_push_after(db->services, after, service)) goto fail; } else if (!queue_push_head(db->services, service)) { goto fail; } service->db = db; service->attributes[0]->handle = handle; service->num_handles = num_handles; /* Fast-forward next_handle if the new service was added to the end */ db->next_handle = MAX(handle + num_handles, db->next_handle); return service->attributes[0]; fail: gatt_db_service_destroy(service); return NULL; }