Module *ns_load_module( const char *modfilename, Client * u ) { static char path[255]; static char loadmodname[255]; struct stat buf; strlcpy( loadmodname, modfilename, 255 ); ns_strlwr( loadmodname ); ircsnprintf( path, 255, "%s/%s%s", MOD_PATH, loadmodname, MOD_STDEXT ); if( stat( path, &buf ) != -1 ) { return load_stdmodule( path, u ); } #ifdef USE_PERL ircsnprintf( path, 255, "%s/%s%s", MOD_PATH, loadmodname, MOD_PERLEXT ); if( stat( path, &buf ) != -1 ) { Module *mod; mod = load_perlmodule( path, u ); if (!mod) { return NULL; } mod->info->build_date = ns_malloc( 10 ); strftime( ( char * )mod->info->build_date, 9, "%d/%m/%y", gmtime( &buf.st_mtime ) ); mod->info->build_time = ns_malloc( 11 ); strftime( ( char * )mod->info->build_time, 10, "%H:%M", gmtime( &buf.st_mtime ) ); return mod; } #endif /* USE_PERL */ /* if we get here, ehhh, doesn't exist */ load_module_error( u, modfilename, __( "Module file not found", u ) ); return NULL; }
int Ns_ModuleInit(char *srv, char *mod) { struct mydata *md = NULL; md = (struct mydata*)ns_malloc(sizeof(struct mydata)); md->modname = strcpy(ns_malloc(strlen(mod)+1), mod); md->server = strcpy(ns_malloc(strlen(srv)+1), srv); return (Ns_TclInitInterps(srv, NsThread_Init, (void*)md) == TCL_OK) ? NS_OK : NS_ERROR; }
void MemThread(void *arg) { int i; void *ptr; Ns_ThreadSetName("memthread"); Ns_MutexLock(&lock); ++nrunning; Ns_CondBroadcast(&cond); while (!memstart) { Ns_CondWait(&cond, &lock); } Ns_MutexUnlock(&lock); ptr = NULL; for (i = 0; i < NA; ++i) { if (arg) { if (ptr) ns_free(ptr); ptr = ns_malloc(10); } else { if (ptr) free(ptr); ptr = malloc(10); } } }
void MemTime(int ns) { Ns_Time start, end, diff; int i; Ns_Thread *tids; tids = ns_malloc(sizeof(Ns_Thread *) * nthreads); Ns_MutexLock(&lock); nrunning = 0; memstart = 0; Ns_MutexUnlock(&lock); printf("starting %d %smalloc threads...", nthreads, ns ? "ns_" : ""); fflush(stdout); Ns_GetTime(&start); for (i = 0; i < nthreads; ++i) { Ns_ThreadCreate(MemThread, (void *) ns, 0, &tids[i]); } Ns_MutexLock(&lock); while (nrunning < nthreads) { Ns_CondWait(&cond, &lock); } printf("waiting...."); fflush(stdout); memstart = 1; Ns_CondBroadcast(&cond); Ns_MutexUnlock(&lock); for (i = 0; i < nthreads; ++i) { Ns_ThreadJoin(&tids[i], NULL); } Ns_GetTime(&end); Ns_DiffTime(&end, &start, &diff); printf("done: %d seconds, %d usec\n", (int) diff.sec, (int) diff.usec); }
void NsCreateConnThread(Pool *poolPtr, int joinThreads) { ConnData *dataPtr; /* * Reap any dead threads. */ if (joinThreads) { NsJoinConnThreads(); } /* * Create a new connection thread. */ dataPtr = ns_malloc(sizeof(ConnData)); dataPtr->poolPtr = poolPtr; dataPtr->connPtr = NULL; Ns_MutexLock(&poolPtr->lock); poolPtr->threads.starting ++; Ns_MutexUnlock(&poolPtr->lock); Ns_ThreadCreate(NsConnThread, dataPtr, 0, &dataPtr->thread); }
int Dci_ListenCallback(char *name, char *addr, int port, Dci_ListenAcceptProc *acceptProc, Dci_ListenExitProc *exitProc, void *arg) { ListenData *lPtr; int sock; int ok; lPtr = ns_malloc(sizeof(ListenData) + strlen(name)); lPtr->acceptProc = acceptProc; lPtr->exitProc = exitProc; lPtr->arg = arg; strcpy(lPtr->name, name); ok = 0; sock = Ns_SockListen(addr, port); if (sock != -1) { Ns_SockSetNonBlocking(sock); if (Ns_SockCallback(sock, ListenProc, lPtr, NS_SOCK_READ|NS_SOCK_EXIT) != NS_OK) { Ns_Log(Error, "Dci_ListenCallback: %s: %s(%d): %s", name, addr, port, strerror(errno)); close(sock); } else { ok = 1; } } Ns_Log(ok ? Notice : Error, "%s: %s on %s:%d", name, ok ? "listening" : "could not listen", addr ? addr : "*", port); if (!ok) { ns_free(lPtr); } return (ok ? NS_OK : NS_ERROR); }
void clean_string( char *text, size_t len ) { char *dd, *start, *orig; size_t i = 0; dd = ns_malloc( len ); start = dd; orig = text; while( *text != '\0' ) { i++; switch( *text ) { case '%': /* if our final length is bigger than the buffer, then we just * drop the char */ if ( ( i + 1 ) <= len ) { *dd++ = '%'; } else { text++; } break; default: break; } *dd++ = *text++; /* Move on to the next char */ } *dd = 0; strncpy( orig, start, len ); ns_free( start ); }
/* lazily maintain 1:1 mapping between tcl and perl interpreters */ perl_context *nsperl2_get_assoc_perl_context (Tcl_Interp *interp) { extern perl_master_context *nsperl2_master_context; assert (nsperl2_master_context); perl_context *context = Tcl_GetAssocData (interp, "nsperl2:perl_context", NULL); PerlInterpreter *perl_interp; if(context) return context; Ns_Log (Notice, "cloning perl interpreter for tcl interp"); PERL_SET_CONTEXT (nsperl2_master_context->perl_master_interp); if ((perl_interp = perl_clone (nsperl2_master_context->perl_master_interp, CLONEf_KEEP_PTR_TABLE)) == NULL) { Ns_Log (Error, "Couldn't clone perl interp"); return NULL; } /* save the perl interp */ context = ns_malloc (sizeof(perl_context)); context->perl_interp = perl_interp; Tcl_SetAssocData(interp, "nsperl2:perl_context", nsperl2_delete_assoc_perl, context); return context; }
void Ns_ThreadCreate(Ns_ThreadProc *proc, void *arg, long stacksize, Ns_Thread *resultPtr) { ThreadArg *argPtr; /* * Determine the stack size and add the guard. */ if (stacksize <= 0) { stacksize = Ns_ThreadStackSize(0); } if (stacksize < stackmin) { stacksize = stackmin; } /* * Create the thread. */ argPtr = ns_malloc(sizeof(ThreadArg)); argPtr->proc = proc; argPtr->arg = arg; argPtr->flags = resultPtr ? 0 : FLAG_DETACHED; strcpy(argPtr->parent, Ns_ThreadGetName()); NsCreateThread(argPtr, stacksize, resultPtr); }
NS_PRIVATE NsError _ns_pixel_proc_flip_optical_axis_all ( NsPixelType pixel_type, nspointer pixels, nssize width, nssize height, nssize length, nssize row_align, NsProgress *progress ) { nspointer start_slice, end_slice, swap_slice; nssize bytes_per_slice; nsfloat iter, num_iters; bytes_per_slice = ns_pixel_buffer_slice_size( pixel_type, width, height, row_align ); if( NULL == ( swap_slice = ns_malloc( bytes_per_slice ) ) ) return ns_error_nomem( NS_ERROR_LEVEL_CRITICAL, NS_MODULE ); start_slice = pixels; end_slice = NS_INCREMENT_POINTER( void, pixels, bytes_per_slice * ( length - 1 ) ); iter = 0.0f; num_iters = ( nsfloat )length / 2.0f; if( NULL != progress ) ns_progress_update( progress, NS_PROGRESS_BEGIN ); while( start_slice < end_slice ) { if( NULL != progress ) { if( ns_progress_cancelled( progress ) ) { ns_free( swap_slice ); return ns_no_error(); } ns_progress_update( progress, 100.0f * ( iter / num_iters ) ); iter += 1.0f; } ns_memcpy( swap_slice, start_slice, bytes_per_slice ); /* swap = start */ ns_memcpy( start_slice, end_slice, bytes_per_slice ); /* start = end */ ns_memcpy( end_slice, swap_slice, bytes_per_slice ); /* end = swap */ start_slice = NS_INCREMENT_POINTER( void, start_slice, bytes_per_slice ); end_slice = NS_DECREMENT_POINTER( void, end_slice, bytes_per_slice ); } if( NULL != progress ) ns_progress_update( progress, NS_PROGRESS_END ); ns_free( swap_slice ); return ns_no_error(); }
lnodepool_t * lnode_pool_create (listcount_t n) { lnodepool_t *pool; lnode_t *nodes; nassert (n != 0); pool = ns_malloc (sizeof *pool); if (!pool) return NULL; nodes = ns_malloc (n * sizeof *nodes); if (!nodes) { ns_free (pool); return NULL; } lnode_pool_init (pool, nodes, n); return pool; }
lnode_t * lnode_create (void *data) { lnode_t *newnode = ns_malloc (sizeof(lnode_t)); if (newnode) { newnode->data = data; newnode->next = NULL; newnode->prev = NULL; } return newnode; }
void Ns_SemaInit(Ns_Sema *semaPtr, int count) { static unsigned int nextid = 0; Sema *sPtr; sPtr = ns_malloc(sizeof(Sema)); sPtr->count = count; NsMutexInitNext(&sPtr->lock, "sm", &nextid); Ns_CondInit(&sPtr->cond); *semaPtr = (Ns_Sema) sPtr; }
void * NsLockAlloc(void) { pthread_mutex_t *lock; int err; lock = ns_malloc(sizeof(pthread_mutex_t)); err = pthread_mutex_init(lock, NULL); if (err != 0) { NsThreadFatal("NsLockAlloc", "pthread_mutex_init", err); } return lock; }
list_t * list_create (listcount_t maxcount) { list_t *newlist = ns_malloc (sizeof *newlist); if (newlist) { nassert (maxcount != 0); newlist->nilnode.next = &newlist->nilnode; newlist->nilnode.prev = &newlist->nilnode; newlist->nodecount = 0; newlist->maxcount = maxcount; } return newlist; }
void Ns_CondInit(Ns_Cond *cond) { pthread_cond_t *condPtr; int err; condPtr = ns_malloc(sizeof(pthread_cond_t)); err = pthread_cond_init(condPtr, NULL); if (err != 0) { NsThreadFatal("Ns_CondInit", "pthread_cond_init", err); } *cond = (Ns_Cond) condPtr; }
static TclCallback * NewCallback(Tcl_Interp *interp, char *proc, char *arg) { TclCallback *cbPtr; char *argv[2]; argv[0] = proc; argv[1] = arg; cbPtr = ns_malloc(sizeof(TclCallback)); cbPtr->server = Ns_TclInterpServer(interp); cbPtr->script = Tcl_Concat(arg ? 2 : 1, argv); return cbPtr; }
NsError ns_pixel_lut_init( NsPixelLut *lut, const NsPixelChannel *channel ) { ns_assert( NULL != lut ); ns_assert( NULL != channel ); ns_assert( NS_PIXEL_CHANNEL_UINT == channel->type ); ns_assert( channel->bits <= 16 ); /* We need to use the actual number of bits (ubits) used in the channel as the table size. */ lut->size = 1 << channel->ubits; lut->values = ns_malloc( lut->size * sizeof( nsuint ) ); return NULL != lut->values ? ns_no_error() : ns_error_nomem( NS_ERROR_LEVEL_CRITICAL, NS_MODULE ); }
char *sstrdup( const char *s ) { char *t; if( s == NULL ) return NULL; t = ns_malloc( strlen( s ) + 1 ); strlcpy( t, s, strlen( s ) + 1 ); if( t == NULL ) { nlog( LOG_CRITICAL, "sstrdup(): out of memory." ); do_exit( NS_EXIT_ERROR, "Out of memory" ); } return t; }
int InitTimers( void ) { timerlist = list_create( TIMER_TABLE_SIZE); if( !timerlist ) { nlog( LOG_CRITICAL, "Unable to create timer hash" ); return NS_FAILURE; } evtimers = ns_malloc( sizeof( struct event ) ); evtimer_set( evtimers, CheckTimers_cb, NULL ); AddTimer(TIMER_TYPE_INTERVAL, PingServers, "PingServers", nsconfig.pingtime, NULL); AddTimer(TIMER_TYPE_INTERVAL, FlushLogs, "FlushLogs", nsconfig.pingtime, NULL); if (nsconfig.setservertimes > 0) AddTimer(TIMER_TYPE_INTERVAL, SetServersTime, "SetServersTime", nsconfig.setservertimes, NULL); AddTimer(TIMER_TYPE_DAILY, ResetLogs, "ResetLogs", 0, NULL); return NS_SUCCESS; }
char *joinbuf( char **av, int ac, int from ) { int i; char *buf; buf = ns_malloc( BUFSIZE ); if( from >= ac ) { dlog( DEBUG1, "joinbuf: from (%d) >= ac (%d)", from, ac ); strlcpy( buf, "( null )", BUFSIZE ); } else { strlcpy( buf, av[from], BUFSIZE ); for( i = from + 1; i < ac; i++ ) { strlcat( buf, " ", BUFSIZE ); strlcat( buf, av[i], BUFSIZE ); } } return( char * ) buf; }
void * Ns_ThreadMalloc(size_t size) { return ns_malloc(size); }
void * Ns_PoolAlloc(Ns_Pool *pool, size_t reqsize) { return ns_malloc(reqsize); }
void adns__querysend_tcp(adns_query qu, struct timeval now) { byte length[2]; #ifndef WIN32 struct iovec iov[2]; #endif int wr, r; adns_state ads; if (qu->ads->tcpstate != server_ok) return; assert(qu->state == query_tcpw); length[0] = (qu->query_dglen & 0x0ff00U) >> 8; length[1] = (qu->query_dglen & 0x0ff); ads = qu->ads; if( !adns__vbuf_ensure( &ads->tcpsend, ads->tcpsend.used + qu->query_dglen + 2 ) ) return; qu->retries++; /* Reset idle timeout. */ ads->tcptimeout.tv_sec = ads->tcptimeout.tv_usec = 0; if (ads->tcpsend.used) { wr = 0; } else { #ifdef WIN32 char *buf = NULL; buf = (char *)ns_malloc((2 + qu->query_dglen)); os_memcpy(buf, length, 2); os_memcpy((buf + 2), qu->query_dgram, qu->query_dglen); wr = os_sock_write(qu->ads->tcpsocket, buf, (2 + qu->query_dglen)); errno = os_sock_errno; ns_free(buf); #else iov[0].iov_base = length; iov[0].iov_len = 2; iov[1].iov_base = qu->query_dgram; iov[1].iov_len = qu->query_dglen; adns__sigpipe_protect(qu->ads); wr = writev(qu->ads->tcpsocket, iov, 2); #endif adns__sigpipe_unprotect(qu->ads); if (wr < 0) { if (! (errno == EAGAIN || errno == OS_SOCK_EINTR || errno == ENOSPC || errno == OS_SOCK_ENOBUFS || errno == ENOMEM)) { adns__tcp_broken(ads, "write", strerror(errno)); return; } wr = 0; } } if( wr < 2 ) { r = adns__vbuf_append( &ads->tcpsend, length, 2 - wr ); assert( r ); wr = 0; } else { wr -= 2; } if ( wr < qu->query_dglen ) { r = adns__vbuf_append( &ads->tcpsend, qu->query_dgram + wr, qu->query_dglen - wr ); assert( r ); } }
void * Ns_Malloc(size_t size) { return ns_malloc(size); }
nspointer ns_io_malloc( nssize bytes, NsIO *io ) { NS_USE_VARIABLE( io ); return ns_malloc( bytes ); }
NS_PRIVATE NsError _ns_pixel_proc_flip_vertical_all ( NsPixelType pixel_type, nspointer pixels, nssize width, nssize height, nssize length, nssize row_align, NsProgress *progress ) { nssize z; nssize bytes_per_row; nssize bytes_per_slice; nspointer row1; nspointer row2; nspointer temp; bytes_per_row = ns_pixel_buffer_row_size( pixel_type, width, row_align ); bytes_per_slice = ns_pixel_buffer_slice_size( pixel_type, width, height, row_align ); if( NULL == ( temp = ns_malloc( bytes_per_row ) ) ) return ns_error_nomem( NS_ERROR_LEVEL_CRITICAL, NS_MODULE ); if( NULL != progress ) ns_progress_update( progress, NS_PROGRESS_BEGIN ); for( z = 0; z < length; ++z ) { if( NULL != progress ) { if( ns_progress_cancelled( progress ) ) break; ns_progress_update( progress, 100.0f * ( ( nsfloat )z ) / ( ( nsfloat )length ) ); } row1 = pixels; row2 = NS_OFFSET_POINTER( void, pixels, ( height - 1 ) * bytes_per_row ); while( row1 < row2 ) { ns_memcpy( temp, row1, bytes_per_row ); /* temp = row1 */ ns_memcpy( row1, row2, bytes_per_row ); /* row1 = row2 */ ns_memcpy( row2, temp, bytes_per_row ); /* row2 = temp */ row1 = NS_INCREMENT_POINTER( void, row1, bytes_per_row ); row2 = NS_DECREMENT_POINTER( void, row2, bytes_per_row ); } pixels = NS_OFFSET_POINTER( void, pixels, bytes_per_slice ); } if( NULL != progress ) ns_progress_update( progress, NS_PROGRESS_END ); ns_free( temp ); return ns_no_error(); }
static void SetUrl(Ns_Request *request, char *url, Tcl_Encoding encoding) { Ns_Conn *conn; Ns_DString ds1, ds2; char *p; Ns_DStringInit(&ds1); Ns_DStringInit(&ds2); /* * Look for a query string at the end of the URL. */ p = strchr(url, '?'); if (p != NULL) { *p++ = '\0'; if (request->query != NULL) { ns_free(request->query); } request->query = ns_strdup(p); } /* * Decode and normalize the URL. If the encoding isn't specified, * use the encoding of the current connection, if any. */ if (encoding == NULL && (conn = Ns_GetConn()) != NULL) { encoding = Ns_ConnGetUrlEncoding(conn); } p = Ns_DecodeUrlWithEncoding(&ds1, url, encoding); if (p == NULL) { p = url; } Ns_NormalizePath(&ds2, p); Ns_DStringTrunc(&ds1, 0); /* * Append a trailing slash to the normalized URL if the original URL * ended in slash that wasn't also the leading slash. */ while (*url == '/') { ++url; } if (*url != '\0' && url[strlen(url) - 1] == '/') { Ns_DStringAppend(&ds2, "/"); } request->url = ns_strdup(ds2.string); Ns_DStringFree(&ds2); /* * Build the urlv and set urlc. */ p = ns_strdup(request->url + 1); Ns_DStringNAppend(&ds1, (char *) &p, sizeof(char *)); while (*p != '\0') { if (*p == '/') { *p++ = '\0'; if (*p == '\0') { /* * Stop on a trailing slash. */ break; } Ns_DStringNAppend(&ds1, (char *) &p, sizeof(char *)); } ++p; } request->urlc = ds1.length / (sizeof(char *)); p = NULL; Ns_DStringNAppend(&ds1, (char *) &p, sizeof(char *)); request->urlv = (char **) ns_malloc((size_t)ds1.length); memcpy(request->urlv, ds1.string, (size_t)ds1.length); Ns_DStringFree(&ds1); }
void WorkThread(void *arg) { int i = (int) arg; int *ip; time_t now; Ns_Thread self; char name[32]; sprintf(name, "-work:%d-", i); Ns_ThreadSetName(name); if (i == 2) { Ns_RWLockWrLock(&rwlock); Msg("rwlock write aquired"); sleep(2); } else { Ns_RWLockRdLock(&rwlock); Msg("rwlock read aquired aquired"); sleep(1); } Ns_CsEnter(&cs); Msg("enter critical section once"); Ns_CsEnter(&cs); Msg("enter critical section twice"); Ns_CsLeave(&cs); Ns_CsLeave(&cs); Ns_ThreadSelf(&self); arg = Ns_TlsGet(&key); Ns_SemaWait(&sema); Msg("got semaphore posted from main"); if (arg == NULL) { arg = ns_malloc(sizeof(int)); Ns_TlsSet(&key, arg); } ip = arg; *ip = i; if (i == 5) { Ns_Time to; int st; Ns_GetTime(&to); Msg("time: %ld %ld", to.sec, to.usec); Ns_IncrTime(&to, 5, 0); Msg("time: %ld %ld", to.sec, to.usec); Ns_MutexLock(&lock); time(&now); Msg("timed wait starts: %s", ns_ctime(&now)); st = Ns_CondTimedWait(&cond, &lock, &to); Ns_MutexUnlock(&lock); time(&now); Msg("timed wait ends: %s - status: %d", ns_ctime(&now), st); } if (i == 9) { Msg("sleep 4 seconds start"); sleep(4); Msg("sleep 4 seconds done"); } time(&now); Ns_RWLockUnlock(&rwlock); Msg("rwlock unlocked"); Msg("exiting"); Ns_ThreadExit((void *) i); }
void * gdbm_malloc(size_t n) { return ns_malloc(n); }