本文整理汇总了C++中CHKmalloc函数的典型用法代码示例。如果您正苦于以下问题:C++ CHKmalloc函数的具体用法?C++ CHKmalloc怎么用?C++ CHKmalloc使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了CHKmalloc函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: addEntry
static inline rsRetVal
addEntry(struct sockaddr_storage *addr, dnscache_entry_t **pEtry)
{
int r;
struct sockaddr_storage *keybuf;
dnscache_entry_t *etry = NULL;
DEFiRet;
CHKmalloc(etry = MALLOC(sizeof(dnscache_entry_t)));
CHKiRet(resolveAddr(addr, etry));
memcpy(&etry->addr, addr, SALEN((struct sockaddr*) addr));
etry->nUsed = 0;
*pEtry = etry;
CHKmalloc(keybuf = malloc(sizeof(struct sockaddr_storage)));
memcpy(keybuf, addr, sizeof(struct sockaddr_storage));
pthread_rwlock_unlock(&dnsCache.rwlock); /* release read lock */
pthread_rwlock_wrlock(&dnsCache.rwlock); /* and re-aquire for writing */
r = hashtable_insert(dnsCache.ht, keybuf, *pEtry);
if(r == 0) {
DBGPRINTF("dnscache: inserting element failed\n");
}
pthread_rwlock_unlock(&dnsCache.rwlock);
pthread_rwlock_rdlock(&dnsCache.rwlock); /* we need this again */
finalize_it:
if(iRet != RS_RET_OK && etry != NULL) {
/* Note: sub-fields cannot be populated in this case */
free(etry);
}
RETiRet;
}
示例2: addEntry
addEntry(struct sockaddr_storage *const addr, dnscache_entry_t **const pEtry)
{
int r;
dnscache_entry_t *etry = NULL;
DEFiRet;
/* entry still does not exist, so add it */
struct sockaddr_storage *const keybuf = malloc(sizeof(struct sockaddr_storage));
CHKmalloc(keybuf);
CHKmalloc(etry = malloc(sizeof(dnscache_entry_t)));
resolveAddr(addr, etry);
assert(etry != NULL);
memcpy(&etry->addr, addr, SALEN((struct sockaddr*) addr));
etry->nUsed = 0;
if(dnscacheEnableTTL) {
etry->validUntil = time(NULL) + dnscacheDefaultTTL;
}
memcpy(keybuf, addr, sizeof(struct sockaddr_storage));
r = hashtable_insert(dnsCache.ht, keybuf, etry);
if(r == 0) {
DBGPRINTF("dnscache: inserting element failed\n");
}
*pEtry = etry;
finalize_it:
if(iRet != RS_RET_OK) {
free(keybuf);
}
RETiRet;
}
示例3: addCounter
/* add a counter to an object
* ctrName is duplicated, caller must free it if requried
* NOTE: The counter is READ-ONLY and MUST NOT be modified (most
* importantly, it must not be initialized, so the caller must
* ensure the counter is properly initialized before AddCounter()
* is called.
*/
static rsRetVal
addCounter(statsobj_t *pThis, uchar *ctrName, statsCtrType_t ctrType, int8_t flags, void *pCtr)
{
ctr_t *ctr;
DEFiRet;
CHKmalloc(ctr = malloc(sizeof(ctr_t)));
ctr->next = NULL;
ctr->prev = NULL;
CHKmalloc(ctr->name = ustrdup(ctrName));
ctr->flags = flags;
ctr->ctrType = ctrType;
switch(ctrType) {
case ctrType_IntCtr:
ctr->val.pIntCtr = (intctr_t*) pCtr;
break;
case ctrType_Int:
ctr->val.pInt = (int*) pCtr;
break;
}
addCtrToList(pThis, ctr);
finalize_it:
RETiRet;
}
示例4: lookupNew
/* create a new lookup table object AND include it in our list of
* lookup tables.
*/
rsRetVal
lookupNew(lookup_ref_t **ppThis)
{
lookup_ref_t *pThis = NULL;
lookup_t *t = NULL;
DEFiRet;
CHKmalloc(pThis = calloc(1, sizeof(lookup_ref_t)));
CHKmalloc(t = calloc(1, sizeof(lookup_t)));
pthread_rwlock_init(&pThis->rwlock, NULL);
pthread_mutex_init(&pThis->reloader_mut, NULL);
pthread_cond_init(&pThis->run_reloader, NULL);
pthread_attr_init(&pThis->reloader_thd_attr);
pThis->do_reload = pThis->do_stop = 0;
pThis->reload_on_hup = 1; /*DO reload on HUP (default)*/
pthread_create(&pThis->reloader, &pThis->reloader_thd_attr, lookupTableReloader, pThis);
pThis->next = NULL;
if(loadConf->lu_tabs.root == NULL) {
loadConf->lu_tabs.root = pThis;
} else {
loadConf->lu_tabs.last->next = pThis;
}
loadConf->lu_tabs.last = pThis;
pThis->self = t;
*ppThis = pThis;
finalize_it:
if(iRet != RS_RET_OK) {
free(t);
free(pThis);
}
RETiRet;
}
示例5: lookupReload
/* this reloads a lookup table. This is done while the engine is running,
* as such the function must ensure proper locking and proper order of
* operations (so that nothing can interfere). If the table cannot be loaded,
* the old table is continued to be used.
*/
static rsRetVal
lookupReload(lookup_t *pThis)
{
uint32_t i;
lookup_t newlu; /* dummy to be able to use support functions without
affecting current settings. */
DEFiRet;
DBGPRINTF("reload requested for lookup table '%s'\n", pThis->name);
memset(&newlu, 0, sizeof(newlu));
CHKmalloc(newlu.name = ustrdup(pThis->name));
CHKmalloc(newlu.filename = ustrdup(pThis->filename));
CHKiRet(lookupReadFile(&newlu));
/* all went well, copy over data members */
pthread_rwlock_wrlock(&pThis->rwlock);
for(i = 0 ; i < pThis->nmemb ; ++i) {
free(pThis->d.strtab[i].key), /* we don't care about exec order of frees */
free(pThis->d.strtab[i].val);
}
free(pThis->d.strtab);
pThis->d.strtab = newlu.d.strtab; /* hand table AND ALL STRINGS over! */
pthread_rwlock_unlock(&pThis->rwlock);
errmsg.LogError(0, RS_RET_OK, "lookup table '%s' reloaded from file '%s'",
pThis->name, pThis->filename);
finalize_it:
free(newlu.name);
free(newlu.filename);
RETiRet;
}
示例6: tcpsrvConstructFinalize
/* ConstructionFinalizer */
static rsRetVal
tcpsrvConstructFinalize(tcpsrv_t *pThis)
{
DEFiRet;
ISOBJ_TYPE_assert(pThis, tcpsrv);
/* prepare network stream subsystem */
CHKiRet(netstrms.Construct(&pThis->pNS));
if(pThis->pszDrvrName != NULL)
CHKiRet(netstrms.SetDrvrName(pThis->pNS, pThis->pszDrvrName));
CHKiRet(netstrms.SetDrvrMode(pThis->pNS, pThis->iDrvrMode));
if(pThis->pszDrvrAuthMode != NULL)
CHKiRet(netstrms.SetDrvrAuthMode(pThis->pNS, pThis->pszDrvrAuthMode));
if(pThis->pPermPeers != NULL)
CHKiRet(netstrms.SetDrvrPermPeers(pThis->pNS, pThis->pPermPeers));
CHKiRet(netstrms.ConstructFinalize(pThis->pNS));
/* set up listeners */
CHKmalloc(pThis->ppLstn = calloc(pThis->iLstnMax, sizeof(netstrm_t*)));
CHKmalloc(pThis->ppLstnPort = calloc(pThis->iLstnMax, sizeof(tcpLstnPortList_t*)));
iRet = pThis->OpenLstnSocks(pThis);
finalize_it:
if(iRet != RS_RET_OK) {
if(pThis->pNS != NULL)
netstrms.Destruct(&pThis->pNS);
errmsg.LogError(0, iRet, "tcpsrv could not create listener (inputname: '%s')",
(pThis->pszInputName == NULL) ? (uchar*)"*UNSET*" : pThis->pszInputName);
}
RETiRet;
}
示例7: getLocalHostname
/* get the name of the local host. A pointer to a character pointer is passed
* in, which on exit points to the local hostname. This buffer is dynamically
* allocated and must be free()ed by the caller. If the functions returns an
* error, the pointer is NULL. This function is based on GNU/Hurd's localhostname
* function.
* rgerhards, 20080-04-10
*/
static rsRetVal
getLocalHostname(uchar **ppName)
{
DEFiRet;
uchar *buf = NULL;
size_t buf_len = 0;
assert(ppName != NULL);
do {
if(buf == NULL) {
buf_len = 128; /* Initial guess */
CHKmalloc(buf = MALLOC(buf_len));
} else {
uchar *p;
buf_len += buf_len;
CHKmalloc(p = realloc (buf, buf_len));
buf = p;
}
} while((gethostname((char*)buf, buf_len) == 0 && !memchr (buf, '\0', buf_len)) || errno == ENAMETOOLONG);
*ppName = buf;
buf = NULL;
finalize_it:
if(iRet != RS_RET_OK) {
if(buf != NULL)
free(buf);
}
RETiRet;
}
示例8: getLocalHostname
/* get the name of the local host. A pointer to a character pointer is passed
* in, which on exit points to the local hostname. This buffer is dynamically
* allocated and must be free()ed by the caller. If the functions returns an
* error, the pointer is NULL.
* This function always tries to return a FQDN, even so be quering DNS. So it
* is safe to assume for the caller that when the function does not return
* a FQDN, it simply is not available. The domain part of that string is
* normalized to lower case. The hostname is kept in mixed case for historic
* reasons.
*/
static rsRetVal
getLocalHostname(uchar **ppName)
{
DEFiRet;
char hnbuf[8192];
uchar *fqdn = NULL;
if(gethostname(hnbuf, sizeof(hnbuf)) != 0) {
strcpy(hnbuf, "localhost");
} else {
hnbuf[sizeof(hnbuf)-1] = '\0'; /* be on the safe side... */
}
char *dot = strstr(hnbuf, ".");
if(dot == NULL) {
/* we need to (try) to find the real name via resolver */
struct hostent *hent = gethostbyname((char*)hnbuf);
if(hent) {
int i = 0;
if(hent->h_aliases) {
const size_t hnlen = strlen(hnbuf);
for(i = 0; hent->h_aliases[i]; i++) {
if(!strncmp(hent->h_aliases[i], hnbuf, hnlen)
&& hent->h_aliases[i][hnlen] == '.') {
break; /* match! */
}
}
}
if(hent->h_aliases && hent->h_aliases[i]) {
CHKmalloc(fqdn = (uchar*)strdup(hent->h_aliases[i]));
} else {
CHKmalloc(fqdn = (uchar*)strdup(hent->h_name));
}
dot = strstr((char*)fqdn, ".");
}
}
if(fqdn == NULL) {
/* already was FQDN or we could not obtain a better one */
CHKmalloc(fqdn = (uchar*) strdup(hnbuf));
}
if(dot != NULL)
for(char *p = dot+1 ; *p ; ++p)
*p = tolower(*p);
*ppName = fqdn;
finalize_it:
RETiRet;
}
示例9: addNewLstnPort
/* add new listener port to listener port list
* rgerhards, 2009-05-21
*/
static inline rsRetVal
addNewLstnPort(tcpsrv_t *pThis, uchar *pszPort, int bSuppOctetFram, uchar *pszAddr)
{
tcpLstnPortList_t *pEntry;
uchar statname[64];
DEFiRet;
ISOBJ_TYPE_assert(pThis, tcpsrv);
/* create entry */
CHKmalloc(pEntry = MALLOC(sizeof(tcpLstnPortList_t)));
CHKmalloc(pEntry->pszPort = ustrdup(pszPort));
pEntry->pszAddr = NULL; // Initalize address to null
/* only if a bind adress is defined copy it in struct */
if (pszAddr != NULL) CHKmalloc(pEntry->pszAddr = ustrdup(pszAddr));
strcpy((char*)pEntry->dfltTZ, (char*)pThis->dfltTZ);
pEntry->bSPFramingFix = pThis->bSPFramingFix;
pEntry->pSrv = pThis;
pEntry->pRuleset = pThis->pRuleset;
pEntry->bSuppOctetFram = bSuppOctetFram;
/* we need to create a property */
CHKiRet(prop.Construct(&pEntry->pInputName));
CHKiRet(prop.SetString(pEntry->pInputName, pThis->pszInputName, ustrlen(pThis->pszInputName)));
CHKiRet(prop.ConstructFinalize(pEntry->pInputName));
/* and add to list */
pEntry->pNext = pThis->pLstnPorts;
pThis->pLstnPorts = pEntry;
/* support statistics gathering */
CHKiRet(statsobj.Construct(&(pEntry->stats)));
snprintf((char*)statname, sizeof(statname), "%s(%s)", pThis->pszInputName, pszPort);
statname[sizeof(statname)-1] = '\0'; /* just to be on the save side... */
CHKiRet(statsobj.SetName(pEntry->stats, statname));
CHKiRet(statsobj.SetOrigin(pEntry->stats, pThis->pszOrigin));
CHKiRet(ratelimitNew(&pEntry->ratelimiter, "tcperver", NULL));
ratelimitSetLinuxLike(pEntry->ratelimiter, pThis->ratelimitInterval, pThis->ratelimitBurst);
ratelimitSetThreadSafe(pEntry->ratelimiter);
STATSCOUNTER_INIT(pEntry->ctrSubmit, pEntry->mutCtrSubmit);
CHKiRet(statsobj.AddCounter(pEntry->stats, UCHAR_CONSTANT("submitted"),
ctrType_IntCtr, CTR_FLAG_RESETTABLE, &(pEntry->ctrSubmit)));
CHKiRet(statsobj.ConstructFinalize(pEntry->stats));
finalize_it:
RETiRet;
}
示例10: doGetGID
/* extract a groupname and return its gid.
* rgerhards, 2007-07-17
*/
static rsRetVal doGetGID(uchar **pp, rsRetVal (*pSetHdlr)(void*, uid_t), void *pVal)
{
struct group *pgBuf = NULL;
struct group gBuf;
DEFiRet;
uchar szName[256];
int bufSize = 2048;
char * stringBuf = NULL;
assert(pp != NULL);
assert(*pp != NULL);
if(getSubString(pp, (char*) szName, sizeof(szName) / sizeof(uchar), ' ') != 0) {
errmsg.LogError(0, RS_RET_NOT_FOUND, "could not extract group name");
ABORT_FINALIZE(RS_RET_NOT_FOUND);
}
CHKmalloc(stringBuf = malloc(bufSize));
while(pgBuf == NULL) {
errno = 0;
getgrnam_r((char*)szName, &gBuf, stringBuf, bufSize, &pgBuf);
if((pgBuf == NULL) && (errno == ERANGE)) {
/* Increase bufsize and try again.*/
bufSize *= 2;
CHKmalloc(stringBuf = realloc(stringBuf, bufSize));
}
}
if(pgBuf == NULL) {
errmsg.LogError(0, RS_RET_NOT_FOUND, "ID for group '%s' could not be found or error", (char*)szName);
iRet = RS_RET_NOT_FOUND;
} else {
if(pSetHdlr == NULL) {
/* we should set value directly to var */
*((gid_t*)pVal) = pgBuf->gr_gid;
} else {
/* we set value via a set function */
CHKiRet(pSetHdlr(pVal, pgBuf->gr_gid));
}
dbgprintf("gid %d obtained for group '%s'\n", (int) pgBuf->gr_gid, szName);
}
skipWhiteSpace(pp); /* skip over any whitespace */
finalize_it:
free(stringBuf);
RETiRet;
}
示例11: lookupNew
/* create a new lookup table object AND include it in our list of
* lookup tables.
*/
static rsRetVal
lookupNew(lookup_ref_t **ppThis)
{
lookup_ref_t *pThis = NULL;
lookup_t *t = NULL;
int initialized = 0;
DEFiRet;
CHKmalloc(pThis = calloc(1, sizeof(lookup_ref_t)));
CHKmalloc(t = calloc(1, sizeof(lookup_t)));
CHKiConcCtrl(pthread_rwlock_init(&pThis->rwlock, NULL));
initialized++; /*1*/
CHKiConcCtrl(pthread_mutex_init(&pThis->reloader_mut, NULL));
initialized++; /*2*/
CHKiConcCtrl(pthread_cond_init(&pThis->run_reloader, NULL));
initialized++; /*3*/
CHKiConcCtrl(pthread_attr_init(&pThis->reloader_thd_attr));
initialized++; /*4*/
pThis->do_reload = pThis->do_stop = 0;
pThis->reload_on_hup = 1; /*DO reload on HUP (default)*/
CHKiConcCtrl(pthread_create(&pThis->reloader, &pThis->reloader_thd_attr, lookupTableReloader, pThis));
initialized++; /*5*/
pThis->next = NULL;
if(loadConf->lu_tabs.root == NULL) {
loadConf->lu_tabs.root = pThis;
} else {
loadConf->lu_tabs.last->next = pThis;
}
loadConf->lu_tabs.last = pThis;
pThis->self = t;
*ppThis = pThis;
finalize_it:
if(iRet != RS_RET_OK) {
errmsg.LogError(errno, iRet, "a lookup table could not be initialized: failed at init-step %d "
"(please enable debug logs for details)", initialized);
if (initialized > 4) lookupStopReloader(pThis);
if (initialized > 3) pthread_attr_destroy(&pThis->reloader_thd_attr);
if (initialized > 2) pthread_cond_destroy(&pThis->run_reloader);
if (initialized > 1) pthread_mutex_destroy(&pThis->reloader_mut);
if (initialized > 0) pthread_rwlock_destroy(&pThis->rwlock);
free(t);
free(pThis);
}
RETiRet;
}
示例12: addEvent
/* add new entry to list. We assume that the fd is not already present and DO NOT check this!
* Returns newly created entry in pEvtLst.
* Note that we currently need to use level-triggered mode, because the upper layers do not work
* in parallel. As such, in edge-triggered mode we may not get notified, because new data comes
* in after we have read everything that was present. To use ET mode, we need to change the upper
* peers so that they immediately start a new wait before processing the data read. That obviously
* requires more elaborate redesign and we postpone this until the current more simplictic mode has
* been proven OK in practice.
* rgerhards, 2009-11-18
*/
static inline rsRetVal
addEvent(nsdpoll_ptcp_t *pThis, int id, void *pUsr, int mode, nsd_ptcp_t *pSock, nsdpoll_epollevt_lst_t **pEvtLst) {
nsdpoll_epollevt_lst_t *pNew;
DEFiRet;
CHKmalloc(pNew = (nsdpoll_epollevt_lst_t*) calloc(1, sizeof(nsdpoll_epollevt_lst_t)));
pNew->id = id;
pNew->pUsr = pUsr;
pNew->pSock = pSock;
pNew->event.events = 0; /* TODO: at some time we should be able to use EPOLLET */
//pNew->event.events = EPOLLET;
if(mode & NSDPOLL_IN)
pNew->event.events |= EPOLLIN;
if(mode & NSDPOLL_OUT)
pNew->event.events |= EPOLLOUT;
pNew->event.data.ptr = pNew;
pthread_mutex_lock(&pThis->mutEvtLst);
pNew->pNext = pThis->pRoot;
pThis->pRoot = pNew;
pthread_mutex_unlock(&pThis->mutEvtLst);
*pEvtLst = pNew;
finalize_it:
RETiRet;
}
示例13: AddParserToList
/* Add a parser to the list. We use a VERY simple and ineffcient algorithm,
* but it is employed only for a few milliseconds during config processing. So
* I prefer to keep it very simple and with simple data structures. Unfortunately,
* we need to preserve the order, but I don't like to add a tail pointer as that
* would require a container object. So I do the extra work to skip to the tail
* when adding elements...
* rgerhards, 2009-11-03
*/
static rsRetVal
AddParserToList(parserList_t **ppListRoot, parser_t *pParser)
{
parserList_t *pThis;
parserList_t *pTail;
DEFiRet;
CHKmalloc(pThis = MALLOC(sizeof(parserList_t)));
pThis->pParser = pParser;
pThis->pNext = NULL;
if(*ppListRoot == NULL) {
pThis->pNext = *ppListRoot;
*ppListRoot = pThis;
} else {
/* find tail first */
for(pTail = *ppListRoot ; pTail->pNext != NULL ; pTail = pTail->pNext)
/* just search, do nothing else */;
/* add at tail */
pTail->pNext = pThis;
}
DBGPRINTF("DDDDD: added parser '%s' to list %p\n", pParser->pName, ppListRoot);
finalize_it:
RETiRet;
}
示例14: rsfrAddFunction
/* add a function to the function registry.
* The handed-over cstr_t* object must no longer be used by the caller.
* A duplicate function name is an error.
* rgerhards, 2009-04-06
*/
static rsRetVal
rsfrAddFunction(uchar *szName, prsf_t rsf)
{
rsf_entry_t *pEntry;
size_t lenName;
DEFiRet;
assert(szName != NULL);
assert(rsf != NULL);
/* first check if we have a duplicate name, with the current approach this means
* we need to go through the whole list.
*/
lenName = strlen((char*)szName);
for(pEntry = funcRegRoot ; pEntry != NULL ; pEntry = pEntry->pNext)
if(!rsCStrSzStrCmp(pEntry->pName, szName, lenName))
ABORT_FINALIZE(RS_RET_DUP_FUNC_NAME);
/* unique name, so add to head of list */
CHKmalloc(pEntry = calloc(1, sizeof(rsf_entry_t)));
CHKiRet(rsCStrConstructFromszStr(&pEntry->pName, szName));
CHKiRet(cstrFinalize(pEntry->pName));
pEntry->rsf = rsf;
pEntry->pNext = funcRegRoot;
funcRegRoot = pEntry;
finalize_it:
if(iRet != RS_RET_OK && iRet != RS_RET_DUP_FUNC_NAME)
free(pEntry);
RETiRet;
}
示例15: AddStrgenToList
/* Add a strgen to the list. We use a VERY simple and ineffcient algorithm,
* but it is employed only for a few milliseconds during config processing. So
* I prefer to keep it very simple and with simple data structures. Unfortunately,
* we need to preserve the order, but I don't like to add a tail pointer as that
* would require a container object. So I do the extra work to skip to the tail
* when adding elements...
*/
static rsRetVal
AddStrgenToList(strgenList_t **ppListRoot, strgen_t *pStrgen)
{
strgenList_t *pThis;
strgenList_t *pTail;
DEFiRet;
CHKmalloc(pThis = MALLOC(sizeof(strgenList_t)));
pThis->pStrgen = pStrgen;
pThis->pNext = NULL;
if(*ppListRoot == NULL) {
pThis->pNext = *ppListRoot;
*ppListRoot = pThis;
} else {
/* find tail first */
for(pTail = *ppListRoot ; pTail->pNext != NULL ; pTail = pTail->pNext)
/* just search, do nothing else */;
/* add at tail */
pTail->pNext = pThis;
}
finalize_it:
RETiRet;
}