void kcdb_buf_dup(kcdb_buf * dest, const kcdb_buf * src) { khm_size cb_buf; khm_size nc_fields; cb_buf = UBOUNDSS(src->cb_used, KCDB_BUF_CBBUF_INITIAL, KCDB_BUF_CBBUF_GROWTH); #if 0 /* replaced by UBOUNDSS() above */ (src->cb_used <= kcdb_cred_initial_size)? kcdb_cred_initial_size: kcdb_cred_initial_size + (((src->cb_used - (kcdb_cred_initial_size + 1)) / kcdb_cred_growth_factor + 1) * kcdb_cred_growth_factor); #endif kcdb_buf_delete(dest); dest->cb_buffer = cb_buf; dest->cb_used = src->cb_used; dest->buffer = PMALLOC(cb_buf); memcpy(dest->buffer, src->buffer, src->cb_used); nc_fields = UBOUNDSS(src->n_fields, KCDB_BUF_FIELDS_INITIAL, KCDB_BUF_FIELDS_GROWTH); dest->nc_fields = nc_fields; dest->n_fields = src->n_fields; dest->fields = PMALLOC(nc_fields * sizeof(dest->fields[0])); memcpy(dest->fields, src->fields, src->n_fields * sizeof(dest->fields[0])); if(dest->n_fields < dest->nc_fields) memset(&(dest->fields[dest->n_fields]), 0, (src->nc_fields - src->n_fields) * sizeof(dest->fields[0])); }
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); }
void k5_add_file_cc(k5_ccc_data * d, wchar_t * epath) { khm_size i; khm_size cch; wchar_t path[MAX_PATH]; if (FAILED(StringCchLength(epath, MAX_PATH, &cch)) || cch == 0) return; StringCbCopy(path, sizeof(path), epath); unexpand_env_var_prefix(path, sizeof(path)); /* see if it's there first */ for (i=0; i < d->n_file_ccs; i++) { if(!_wcsicmp(d->file_ccs[i].path, path)) return; } if (d->n_file_ccs == d->nc_file_ccs) { k5_file_cc * f; d->nc_file_ccs = UBOUNDSS(d->n_file_ccs + 1, K5_FCC_ALLOC_INCR, K5_FCC_ALLOC_INCR); #ifdef DEBUG assert(d->nc_file_ccs > d->n_file_ccs); #endif f = PMALLOC(sizeof(*f) * d->nc_file_ccs); ZeroMemory(f, sizeof(*f) * d->nc_file_ccs); if (d->n_file_ccs > 0) { #ifdef DEBUG assert(d->file_ccs != NULL); #endif memcpy(f, d->file_ccs, sizeof(*f) * d->n_file_ccs); } if (d->file_ccs) PFREE(d->file_ccs); d->file_ccs = f; } StringCbCopy(d->file_ccs[d->n_file_ccs].path, sizeof(d->file_ccs[0].path), path); if(PathFileExists(path)) d->file_ccs[d->n_file_ccs].flags = K5_FCC_FLAG_EXISTS; else d->file_ccs[d->n_file_ccs].flags = 0; d->n_file_ccs++; }
void kcdb_buf_new(kcdb_buf * buf, khm_size n_fields) { buf->buffer = PMALLOC(KCDB_BUF_CBBUF_INITIAL); buf->cb_buffer = KCDB_BUF_CBBUF_INITIAL; buf->cb_used = 0; if(n_fields == KCDB_BUF_DEFAULT) n_fields = KCDB_BUF_FIELDS_INITIAL; assert(n_fields < KCDB_BUF_MAX_SLOTS); buf->n_fields = n_fields; buf->nc_fields = UBOUNDSS(n_fields, KCDB_BUF_FIELDS_INITIAL, KCDB_BUF_FIELDS_GROWTH); buf->fields = PMALLOC(sizeof(buf->fields[0]) * buf->n_fields); ZeroMemory(buf->fields, sizeof(buf->fields[0]) * buf->n_fields); }
static cfg_node_data * get_node_data(khui_config_node_i * node, void * key, khm_boolean create) { khm_size i; for (i=0; i<node->n_data; i++) { if (node->data[i].key == key) return &(node->data[i]); } if (!create) return NULL; if (node->n_data + 1 > node->nc_data) { cfg_node_data * newdata; node->nc_data = UBOUNDSS((node->n_data + 1), KHUI_NODEDATA_ALLOC_INCR, KHUI_NODEDATA_ALLOC_INCR); #ifdef DEBUG assert(node->nc_data >= node->n_data + 1); #endif newdata = PMALLOC(sizeof(*newdata) * node->nc_data); #ifdef DEBUG assert(newdata); #endif ZeroMemory(newdata, sizeof(*newdata) * node->nc_data); if (node->data && node->n_data > 0) { memcpy(newdata, node->data, node->n_data * sizeof(*newdata)); PFREE(node->data); } node->data = newdata; } node->data[node->n_data].key = key; node->data[node->n_data].hwnd = NULL; node->data[node->n_data].param = 0; node->data[node->n_data].flags = 0; node->n_data++; return &(node->data[node->n_data - 1]); }
static void kcdb_buf_assert_size(kcdb_buf * buf, khm_size cbsize) { khm_size new_size; void * new_buf; /* should be less than or equal to the max signed 32 bit int */ assert(cbsize <= KHM_INT32_MAX); if(cbsize <= buf->cb_buffer) return; new_size = UBOUNDSS(cbsize, KCDB_BUF_CBBUF_INITIAL, KCDB_BUF_CBBUF_GROWTH); assert(new_size > buf->cb_buffer && new_size > 0); new_buf = PMALLOC(new_size); assert(new_buf != NULL); memcpy(new_buf, buf->buffer, buf->cb_used); PFREE(buf->buffer); buf->buffer = new_buf; }
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); } } }
void kcdb_buf_alloc(kcdb_buf * buf, khm_size slot, khm_ui_2 id, khm_size cbsize) { khm_size cbnew; khm_ssize cbdelta; khm_size cbold; kcdb_buf_field * f; cbnew = UBOUND32(cbsize); assert(slot <= KCDB_BUF_APPEND); if(slot == KCDB_BUF_APPEND) { slot = kcdb_buf_slot_by_id(buf, id); if(slot == KCDB_BUF_INVALID_SLOT) slot = buf->n_fields; } assert(slot < KCDB_BUF_MAX_SLOTS); if((slot + 1) > buf->nc_fields) { kcdb_buf_field * nf; khm_size ns; ns = UBOUNDSS((slot + 1), KCDB_BUF_FIELDS_INITIAL, KCDB_BUF_FIELDS_GROWTH); nf = PMALLOC(sizeof(buf->fields[0]) * ns); memcpy(nf, buf->fields, sizeof(buf->fields[0]) * buf->n_fields); if(ns > buf->n_fields) memset(&(nf[buf->n_fields]), 0, sizeof(buf->fields[0]) * (ns - buf->n_fields)); PFREE(buf->fields); buf->fields = nf; buf->nc_fields = ns; } if((slot + 1) > buf->n_fields) buf->n_fields = slot + 1; f = &(buf->fields[slot]); if(f->flags & KCDB_CREDF_FLAG_ALLOCD) { /* there's already an allocation. we have to resize it to accomodate the new size */ cbold = UBOUND32(f->cbsize); /* demote before substraction */ cbdelta = ((khm_ssize) cbnew) - (khm_ssize) cbold; if(cbnew > cbold) { kcdb_buf_assert_size(buf, buf->cb_used + cbdelta); } if(buf->cb_used > f->offset + cbold) { khm_size i; memmove( ((BYTE *) buf->buffer) + (f->offset + cbnew), ((BYTE *) buf->buffer) + (f->offset + cbold), buf->cb_used - (f->offset + cbold)); for(i=0; i < (int) buf->n_fields; i++) { if(i != slot && (buf->fields[i].flags & KCDB_CREDF_FLAG_ALLOCD) && buf->fields[i].offset > f->offset) { buf->fields[i].offset = (khm_ui_4)(((khm_ssize) buf->fields[i].offset) + cbdelta); } } } /* demote integer before adding signed quantity */ buf->cb_used = (khm_size)(((khm_ssize) buf->cb_used) + cbdelta); f->cbsize = (khm_ui_4) cbsize; } else { kcdb_buf_assert_size(buf, buf->cb_used + cbnew); f->offset = (khm_ui_4) buf->cb_used; f->cbsize = (khm_ui_4) cbsize; buf->cb_used += cbnew; } if(cbsize == 0) { f->flags &= ~KCDB_CREDF_FLAG_ALLOCD; f->flags &= ~KCDB_CREDF_FLAG_DATA; f->id = KCDB_BUFF_ID_INVALID; } else { f->flags |= KCDB_CREDF_FLAG_ALLOCD; f->id = id; } }
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]; }