static void add_ctx_handler_node(const kherr_handler_node * n) { khm_size idx; EnterCriticalSection(&cs_error); /* Make sure we have enough space */ if (n_ctx_handlers + 1 > nc_ctx_handlers) { nc_ctx_handlers = UBOUNDSS(n_ctx_handlers + 1, CTX_ALLOC_INCR, CTX_ALLOC_INCR); ctx_handlers = PREALLOC(ctx_handlers, sizeof(*ctx_handlers) * nc_ctx_handlers); } /* Since commit events are the most frequent, we put those handlers at the top of the list. When dispatching a commit event, we stop looking at the list when we find a filter that doesn't filter for commit events. */ if (n->filter & KHERR_CTX_EVTCOMMIT) { idx = 0; if (n_ctx_handlers > 0) memmove(&ctx_handlers[1], &ctx_handlers[0], n_ctx_handlers * sizeof(ctx_handlers[0])); } else { idx = n_ctx_handlers; } n_ctx_handlers++; ctx_handlers[idx] = *n; if (ctx_handlers[idx].filter == 0) ctx_handlers[idx].filter = KHERR_CTX_BEGIN | KHERR_CTX_DESCRIBE | KHERR_CTX_END | KHERR_CTX_ERROR | KHERR_CTX_NEWCHILD | KHERR_CTX_FOLDCHILD; if (ctx_handlers[idx].serial == KHERR_SERIAL_CURRENT) { kherr_context * c; c = peek_context(); if (IS_KHERR_CTX(c)) { ctx_handlers[idx].serial = c->serial; } else { /* If we were going to refer to the current context and there is no current context, then we don't want to keep this handler node. If we do, it will never get called and will not be automatically removed from the list. */ remove_ctx_handler_by_index(idx); } } LeaveCriticalSection(&cs_error); }
static void cfgui_sync_node(cfgui_wnd_data * d, HWND hwtv, khui_config_node c, HTREEITEM hItem) { khui_config_node child; HTREEITEM hChild; struct cfgui_child_info * childinfo = NULL; khm_size n_childinfo = 0; khm_size nc_childinfo = 0; khm_size i; /* first, get the list of children from the treeview control */ for (hChild = TreeView_GetChild(hwtv, hItem); hChild; hChild = TreeView_GetNextSibling(hwtv, hChild)) { if (n_childinfo >= nc_childinfo) { nc_childinfo = UBOUNDSS(n_childinfo + 1, CI_ALLOC_INCR, CI_ALLOC_INCR); #ifdef DEBUG assert(nc_childinfo > n_childinfo); #endif childinfo = PREALLOC(childinfo, sizeof(*childinfo) * nc_childinfo); #ifdef DEBUG assert(childinfo); #endif } ZeroMemory(&childinfo[n_childinfo], sizeof(childinfo[n_childinfo])); childinfo[n_childinfo].hItem = hChild; childinfo[n_childinfo].checked = FALSE; n_childinfo++; } /* now, go through the list of actual nodes and make sure they match up */ child = NULL; for (khui_cfg_get_first_child(c, &child); child; khui_cfg_get_next_release(&child)) { hChild = (HTREEITEM) khui_cfg_get_param(child); for (i=0; i < n_childinfo; i++) { if (childinfo[i].hItem == hChild) break; } if (i < n_childinfo) { childinfo[i].checked = TRUE; } else { /* add it to the list, so we can create the node in the tree view control later. */ if (n_childinfo >= nc_childinfo) { nc_childinfo = UBOUNDSS(n_childinfo + 1, CI_ALLOC_INCR, CI_ALLOC_INCR); #ifdef DEBUG assert(nc_childinfo > n_childinfo); #endif childinfo = PREALLOC(childinfo, sizeof(*childinfo) * nc_childinfo); #ifdef DEBUG assert(childinfo); #endif } ZeroMemory(&childinfo[n_childinfo], sizeof(childinfo[n_childinfo])); childinfo[n_childinfo].node = child; khui_cfg_hold(child); n_childinfo++; } } /* by this point, the childinfo list contains items of the following forms: 1. childinfo[i].hItem != NULL && childinfo[i].checked == TRUE Corresponds to a tree view item that has a matching configuration node. Nothing to do here. 2. childinfo[i].hItem != NULL && childinfo[i].checked == FALSE Corresponds to a tree view item that has no matching configuration node. These should be removed. 3. childinfo[i].hItem == NULL && childinfo[i].node != NULL Corresponds to a configuration node that has no matching tree view item. These nodes should be added. */ /* first do the removals */ for (i=0; i < n_childinfo; i++) { if (childinfo[i].hItem == NULL) break; /* nothing more to see from this point on */ if (!childinfo[i].checked) { /* remove! */ cfgui_remove_item(hwtv, childinfo[i].hItem); } } /* continue from where the previous loop left off */ for (; i < n_childinfo; i++) { #ifdef DEBUG assert(childinfo[i].hItem == NULL); assert(childinfo[i].node != NULL); #endif cfgui_add_node(d, hwtv, childinfo[i].node, c, FALSE); khui_cfg_release(childinfo[i].node); childinfo[i].node = NULL; } if (childinfo) PFREE(childinfo); /* finally recurse through to the next level */ for (hChild = TreeView_GetChild(hwtv, hItem); hChild; hChild = TreeView_GetNextSibling(hwtv, hChild)) { TVITEMEX itemex; ZeroMemory(&itemex, sizeof(itemex)); itemex.mask = TVIF_PARAM; itemex.hItem = hChild; TreeView_GetItem(hwtv, &itemex); if (itemex.lParam) { child = (khui_config_node) itemex.lParam; cfgui_sync_node(d, hwtv, child, hChild); } } }
static ident_data * find_ident_by_node(khui_config_node node) { khm_size cb; wchar_t idname[KCDB_IDENT_MAXCCH_NAME]; int i; khm_handle ident = NULL; cb = sizeof(idname); khui_cfg_get_name(node, idname, &cb); for (i=0; i < (int)cfg_idents.n_idents; i++) { if (!wcscmp(cfg_idents.idents[i].idname, idname)) break; } if (i < (int)cfg_idents.n_idents) return &cfg_idents.idents[i]; /* there is no identity data for this configuration node. We try to create it. */ if (KHM_FAILED(kcdb_identity_create(idname, 0, &ident))) return NULL; if (cfg_idents.n_idents >= cfg_idents.nc_idents) { cfg_idents.nc_idents = UBOUNDSS(cfg_idents.n_idents + 1, IDENTS_DATA_ALLOC_INCR, IDENTS_DATA_ALLOC_INCR); #ifdef DEBUG assert(cfg_idents.nc_idents > cfg_idents.n_idents); #endif cfg_idents.idents = PREALLOC(cfg_idents.idents, sizeof(*cfg_idents.idents) * cfg_idents.nc_idents); #ifdef DEBUG assert(cfg_idents.idents); #endif ZeroMemory(&(cfg_idents.idents[cfg_idents.n_idents]), sizeof(*cfg_idents.idents) * (cfg_idents.nc_idents - cfg_idents.n_idents)); } i = (int) cfg_idents.n_idents; StringCbLength(idname, KCDB_IDENT_MAXCB_NAME, &cb); cb += sizeof(wchar_t); cfg_idents.idents[i].idname = PMALLOC(cb); #ifdef DEBUG assert(cfg_idents.idents[i].idname); #endif StringCbCopy(cfg_idents.idents[i].idname, cb, idname); cfg_idents.idents[i].ident = ident; cfg_idents.idents[i].removed = FALSE; kcdb_identity_get_flags(ident, &cfg_idents.idents[i].flags); #ifdef DEBUG assert(cfg_idents.idents[i].flags & KCDB_IDENT_FLAG_CONFIG); #endif read_params_ident(&cfg_idents.idents[i]); cfg_idents.n_idents++; /* leave ident held. */ return &cfg_idents.idents[i]; }
/*================================================================ * XmlBufferSetSize * * Increases or decreases buffer cap so that at least * 'new_length' bytes can be stored * * On error, m's fields do not change. * * returns: * UPNP_E_SUCCESS * UPNP_E_OUTOF_MEMORY * *=================================================================*/ static int XmlBufferSetSize( Pool * pool, XmlBuffer * m, IN size_t new_length ) { size_t diff; size_t alloc_len; char *temp_buf; if ( new_length > m->length ) // increase length { // need more mem? if ( new_length < m->capacity ) { return 0; // have enough mem; done } if (m->length == 0) { alloc_len = new_length + 1; temp_buf = PMALLOC(alloc_len); if ( temp_buf == NULL ) return XML_INSUFFICIENT_MEMORY; m->buf = temp_buf; m->capacity = alloc_len; return 0; } diff = new_length - m->length + 1; alloc_len = MAXVAL( m->size_inc, diff ) + m->capacity; } else // decrease length { assert( new_length <= m->length ); return 0; /* // if diff is 0..m->size_inc, don't PFREE if( ( m->capacity - new_length ) <= m->size_inc ) { return 0; } alloc_len = new_length + m->size_inc; */ } assert( alloc_len > new_length ); temp_buf = PREALLOC( m->buf, m->length, alloc_len ); if ( temp_buf == NULL ) { // try smaller size alloc_len = new_length + 1; temp_buf = PREALLOC( m->buf, m->length, alloc_len); if ( temp_buf == NULL ) { return XML_INSUFFICIENT_MEMORY; } } // save m->buf = temp_buf; m->capacity = alloc_len; return 0; }