/* make the allocations based on the largest need by the three comms: 1- transfer an atom who has moved into other proc's domain (mpi_atom) 2- exchange boundary atoms (boundary_atom) 3- update position info for boundary atoms (mpi_rvec) the largest space by far is required for the 2nd comm operation above. buffers are void*, type cast to the correct pointer type to access the allocated buffers */ int Allocate_MPI_Buffers( mpi_datatypes *mpi_data, int est_recv, neighbor_proc *my_nbrs, char *msg ) { int i; mpi_out_data *mpi_buf; MPI_Comm comm; comm = mpi_data->world; /* in buffers */ mpi_data->in1_buffer = (void*) scalloc( est_recv, sizeof(boundary_atom), "in1_buffer", comm ); mpi_data->in2_buffer = (void*) scalloc( est_recv, sizeof(boundary_atom), "in2_buffer", comm ); /* out buffers */ for( i = 0; i < MAX_NBRS; ++i ) { mpi_buf = &( mpi_data->out_buffers[i] ); /* allocate storage for the neighbor processor i */ mpi_buf->index = (int*) scalloc( my_nbrs[i].est_send, sizeof(int), "mpibuf:index", comm ); mpi_buf->out_atoms = (void*) scalloc( my_nbrs[i].est_send, sizeof(boundary_atom), "mpibuf:out_atoms", comm ); } return SUCCESS; }
int main(int argc, char ** argv) { assert(argc == 3); uint64_t msg_size = atoi(argv[1]); int loop_length = atoi(argv[2]); bsparrow_t * bsp = bsparrow_new(50000, 4000, 2, 2, 1, "9003"); bsparrow_event_t bspev; bsparrow_socket_t * bsock; bsparrow_wait(bsp, &bspev, 0); if(bspev.event & 16) { bsock = bspev.bsock; bsparrow_socket_assign_id(bsock, 1); } else { exit(-1); } sparrow_msg_t msg; int j = 0; int64_t time = now(); while(j < loop_length) { int i = 0; while(i < 10000) { if(i == 5000) { char *data = scalloc(1, 100); uint64_t temp = 92; memcpy(data, &temp, 8); sprintf(data + 8,"Got 50, need mooooreee!"); bsparrow_send(bsp, bsock, &data, 100); Dprintf("I am sending an aknowledge msg at msg number: %lu\n", j*100 + 50); } get_msg(bsp, bsock, &bspev, &msg); Dprintf("i: %d\n", i); Dprintf("Remaining length: %lu\n", bspev.total_length -msg_size); printmsg(msg); i++; } Dprintf("j: %d\n", j); Dprintf("Remaining length: %lu\n", bspev.total_length -msg_size); j++; } printf("Sending: Got them all, thanks!\n"); char *data = scalloc(1, 100); uint64_t temp = 92; memcpy(data, &temp, 8); sprintf(data + 8, "Got them all, thanks!"); bsparrow_send(bsp, bsock, &data, 100); results(j*10000, time, msg_size); bsparrow_destroy(bsp); return 0; }
int open_new_qseqs(Settings *opts, Trace_reader *trc, Spot_info *spot) { size_t name_len; char *name; int i; if (NULL == trc->qseqs) { name_len = strlen(opts->qseq_dir) + strlen(opts->qseq_suffix) + 64; name = smalloc(name_len); for (i = 0; ; i++) { snprintf(name, name_len, "%s/s_%d_%d_%04d_%s", opts->qseq_dir, spot->lane, i + 1, spot->tile, opts->qseq_suffix); if (0 != access(name, F_OK)) { if (ENOENT == errno) break; printf("Error looking up %s: %s\n", name, strerror(errno)); free(name); return -1; } } if (0 == i) { printf("Couldn't find qseq files\n"); free(name); return -1; } trc->nqseqs = i; trc->qseqs = scalloc(i, sizeof(FILE *)); trc->qseq_names = scalloc(i, sizeof(char *)); free(name); } else { if (0 != close_qseq_files(trc)) return -1; } for (i = 0; i < trc->nqseqs; i++) { trc->qseq_names[i] = aprintf("%s/s_%d_%d_%04d_%s", opts->qseq_dir, spot->lane, i + 1, spot->tile, opts->qseq_suffix); trc->qseqs[i] = fopen(trc->qseq_names[i], "r"); if (NULL == trc->qseqs[i]) { printf("Couldn't open %s: %s\n", trc->qseq_names[i], strerror(errno)); return -1; } } return 0; }
/* * Build an i3String from an UTF-8 encoded string with fixed length. * To be used when no proper NUL-terminaison is available. * Returns the newly-allocated i3String. * */ i3String *i3string_from_utf8_with_length(const char *from_utf8, size_t num_bytes) { i3String *str = scalloc(sizeof(i3String)); /* Copy the actual text to our i3String */ str->utf8 = scalloc(sizeof(char) * (num_bytes + 1)); strncpy(str->utf8, from_utf8, num_bytes); str->utf8[num_bytes] = '\0'; /* Store the length */ str->num_bytes = num_bytes; return str; }
/* * This function resolves ~ in pathnames. * It may resolve wildcards in the first part of the path, but if no match * or multiple matches are found, it just returns a copy of path as given. * */ char *resolve_tilde(const char *path) { static glob_t globbuf; char *head, *tail, *result; tail = strchr(path, '/'); head = strndup(path, tail ? (size_t)(tail - path) : strlen(path)); int res = glob(head, GLOB_TILDE, NULL, &globbuf); free(head); /* no match, or many wildcard matches are bad */ if (res == GLOB_NOMATCH || globbuf.gl_pathc != 1) result = sstrdup(path); else if (res != 0) { die("glob() failed"); } else { head = globbuf.gl_pathv[0]; result = scalloc(strlen(head) + (tail ? strlen(tail) : 0) + 1); strncpy(result, head, strlen(head)); if (tail) strncat(result, tail, strlen(tail)); } globfree(&globbuf); return result; }
/* allocate space for my_atoms important: we cannot know the exact number of atoms that will fall into a process's box throughout the whole simulation. therefore we need to make upper bound estimates for various data structures */ int PreAllocate_Space( reax_system *system, control_params *control, storage *workspace, MPI_Comm comm ) { int i; /* determine the local and total capacity */ system->local_cap = MAX( (int)(system->n * SAFE_ZONE), MIN_CAP ); system->total_cap = MAX( (int)(system->N * SAFE_ZONE), MIN_CAP ); #if defined(DEBUG) fprintf( stderr, "p%d: local_cap=%d total_cap=%d\n", system->my_rank, system->local_cap, system->total_cap ); #endif system->my_atoms = (reax_atom*) scalloc( system->total_cap, sizeof(reax_atom), "my_atoms", comm ); /* space for keeping restriction info, if any */ // not yet implemented in the parallel version!!! // if( control->restrict_bonds ) { // workspace->restricted = (int*) // scalloc( system->local_cap, sizeof(int), "restricted_atoms", comm ); // workspace->restricted_list = (int**) // scalloc( system->local_cap, sizeof(int*), "restricted_list", comm ); // for( i = 0; i < system->local_cap; ++i ) // workspace->restricted_list[i] = (int*) // scalloc( MAX_RESTRICT, sizeof(int), "restricted_list[i]", comm ); // } return SUCCESS; }
static void tdpotn_get_all_degree(int *sp, int N, int **alld, int *alldNum, double **p_alld, double rate_airedgeChoose) { int *ddis = scalloc(N, sizeof(int)); int i; for (i=0; i<N; ++i) { if (sp[i] > 0) { ddis[sp[i]]++; } } *alld = smalloc(N*sizeof(int)); *alldNum = 0; for (i=2; i<N; ++i) { if (ddis[i]) { (*alld)[(*alldNum)++] = i; } } free(ddis); *p_alld = malloc((*alldNum)*sizeof(double)); for (i=0; i<*alldNum; ++i) { (*p_alld)[i] = pow((*alld)[i], 0-rate_airedgeChoose); } double total = 0; for (i=0; i<*alldNum; ++i) { total += (*p_alld)[i]; } for (i=0; i<*alldNum; ++i) { (*p_alld)[i] /= total; } for (i=1; i<*alldNum; ++i) { (*p_alld)[i] += (*p_alld)[i-1]; } }
/* * Generates a configure_notify event and sends it to the given window * Applications need this to think they’ve configured themselves correctly. * The truth is, however, that we will manage them. * */ void fake_configure_notify(xcb_connection_t *conn, xcb_rectangle_t r, xcb_window_t window, int border_width) { /* Every X11 event is 32 bytes long. Therefore, XCB will copy 32 bytes. * In order to properly initialize these bytes, we allocate 32 bytes even * though we only need less for an xcb_configure_notify_event_t */ void *event = scalloc(32); xcb_configure_notify_event_t *generated_event = event; generated_event->event = window; generated_event->window = window; generated_event->response_type = XCB_CONFIGURE_NOTIFY; generated_event->x = r.x; generated_event->y = r.y; generated_event->width = r.width; generated_event->height = r.height; generated_event->border_width = border_width; generated_event->above_sibling = XCB_NONE; generated_event->override_redirect = false; xcb_send_event(conn, false, window, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char *)generated_event); xcb_flush(conn); free(event); }
static r_type_ptr createFromBoolean(int I, type_info* ti) { int *space = scalloc(1, sizeof(int)); *space = I; int** ret = salloc(sizeof(int*)); *ret = space; return (r_type_ptr)ret; }
void add_ignore(const char *nick, time_t delta) { IgnoreData *ign; char who[NICKMAX]; time_t now = time(NULL); IgnoreData **whichlist = &ignore[tolower(nick[0])]; strscpy(who, nick, NICKMAX); for (ign = *whichlist; ign; ign = ign->next) { if (stricmp(ign->who, who) == 0) break; } if (ign) { if (ign->time > now) ign->time += delta; else ign->time = now + delta; } else { ign = scalloc(sizeof(*ign), 1); strscpy(ign->who, who, sizeof(ign->who)); ign->time = now + delta; ign->next = *whichlist; *whichlist = ign; } }
int split_buf(char *buf, char ***argv, int colon_special) { int argvsize = 8; int argc; char *s; *argv = scalloc(sizeof(char *) * argvsize, 1); argc = 0; while (*buf) { if (argc == argvsize) { argvsize += 8; *argv = srealloc(*argv, sizeof(char *) * argvsize); } if (*buf == ':') { (*argv)[argc++] = buf + 1; buf = ""; } else { s = strpbrk(buf, " "); if (s) { *s++ = 0; while (*s == ' ') s++; } else { s = buf + strlen(buf); } (*argv)[argc++] = buf; buf = s; } } return argc; }
/* * Converts the given string to UTF-8 from UCS-2 big endian. The return value * must be freed after use. * */ char *convert_ucs2_to_utf8(xcb_char2b_t *text, size_t num_glyphs) { /* Allocate the output buffer (UTF-8 is at most 4 bytes per glyph) */ size_t buffer_size = num_glyphs * 4 + 1; char *buffer = scalloc(buffer_size, 1); /* We need to use an additional pointer, because iconv() modifies it */ char *output = buffer; size_t output_size = buffer_size - 1; if (utf8_conversion_descriptor == (iconv_t)-1) { /* Get a new conversion descriptor */ utf8_conversion_descriptor = iconv_open("UTF-8", "UCS-2BE"); if (utf8_conversion_descriptor == (iconv_t)-1) err(EXIT_FAILURE, "Error opening the conversion context"); } else { /* Reset the existing conversion descriptor */ iconv(utf8_conversion_descriptor, NULL, NULL, NULL, NULL); } /* Do the conversion */ size_t input_len = num_glyphs * sizeof(xcb_char2b_t); size_t rc = iconv(utf8_conversion_descriptor, (char **)&text, &input_len, &output, &output_size); if (rc == (size_t)-1) { perror("Converting to UTF-8 failed"); free(buffer); return NULL; } return buffer; }
static User *new_user(const char *nick) { User *user, **list; user = scalloc(sizeof(User), 1); if (!nick) nick = ""; strscpy(user->nick, nick, NICKMAX); list = &userlist[HASH(user->nick)]; user->next = *list; if (*list) (*list)->prev = user; *list = user; user->na = findnick(nick); if (user->na) user->na->u = user; usercnt++; if (usercnt > maxusercnt) { maxusercnt = usercnt; maxusertime = time(NULL); if (LogMaxUsers) alog("user: New maximum user count: %d", maxusercnt); } user->isSuperAdmin = 0; /* always set SuperAdmin to 0 for new users */ user->nickTrack = NULL; /* ensure no default tracking nick */ return user; }
/* * Build an i3String from an UCS-2 encoded string. * Returns the newly-allocated i3String. * */ i3String *i3string_from_ucs2(const xcb_char2b_t *from_ucs2, size_t num_glyphs) { i3String *str = scalloc(sizeof(i3String)); /* Copy the actual text to our i3String */ size_t num_bytes = num_glyphs * sizeof(xcb_char2b_t); str->ucs2 = scalloc(num_bytes); memcpy(str->ucs2, from_ucs2, num_bytes); /* Store the length */ str->num_glyphs = num_glyphs; str->utf8 = NULL; str->num_bytes = 0; return str; }
void common_unban2(ChannelInfo * ci, char *nick) { User *u; char *av[3], **bans; int count, i; if (!ci || !nick) return; else if (!(u = finduser(nick))) return; av[0] = ci->name; av[1] = sstrdup("-b"); count = ci->c->bancount; bans = scalloc(sizeof(char *) * count, 1); memcpy(bans, ci->c->bans, sizeof(char *) * count); for (i = 0; i < count; i++) { if (match_usermask2(bans[i], u)) { anope_cmd_mode(whosends(ci), ci->name, "-b %s", bans[i]); av[2] = bans[i]; do_cmode(whosends(ci), 3, av); } } free(bans); free(av[1]); }
static r_type_ptr createBoolean(type_info* ti) { int* t = scalloc(1, sizeof(int)); *t = 0; int** ret = salloc(sizeof(int*)); *ret = t; return (r_type_ptr)ret; }
int add_session(char *nick, char *host, char *hostip) { Session *session, **list; Exception *exception; int sessionlimit = 0; session = findsession(host); if (session) { exception = find_hostip_exception(host, hostip); if (checkDefCon(DEFCON_REDUCE_SESSION)) { sessionlimit = exception ? exception->limit : DefConSessionLimit; } else { sessionlimit = exception ? exception->limit : DefSessionLimit; } if (sessionlimit != 0 && session->count >= sessionlimit) { if (SessionLimitExceeded) notice(s_OperServ, nick, SessionLimitExceeded, host); if (SessionLimitDetailsLoc) notice(s_OperServ, nick, "%s", SessionLimitDetailsLoc); /* We don't use kill_user() because a user stucture has not yet * been created. Simply kill the user. -TheShadow */ kill_user(s_OperServ, nick, "Session limit exceeded"); session->hits++; if (MaxSessionKill && session->hits >= MaxSessionKill) { char akillmask[BUFSIZE]; snprintf(akillmask, sizeof(akillmask), "*@%s", host); add_akill(NULL, akillmask, s_OperServ, time(NULL) + SessionAutoKillExpiry, "Session limit exceeded"); anope_cmd_global(s_OperServ, "Added a temporary AKILL for \2%s\2 due to excessive connections", akillmask); } return 0; } else { session->count++; return 1; } } nsessions++; session = scalloc(sizeof(Session), 1); session->host = sstrdup(host); list = &sessionlist[HASH(session->host)]; session->next = *list; if (*list) (*list)->prev = session; *list = session; session->count = 1; return 1; }
/* debug calloc: scalloc() with memory management */ void *d_calloc( size_t elsize, size_t els ) { void *p = scalloc( elsize, els ); clog( CL_DEBUG, "scalloc(%d,%d) -> %p", elsize, els, p ); return p; }
static database_handle_t *opensex_db_open_read(const char *filename) { database_handle_t *db; opensex_t *rs; FILE *f; int errno1; char path[BUFSIZE]; snprintf(path, BUFSIZE, "%s/%s", datadir, filename != NULL ? filename : "services.db"); f = fopen(path, "r"); if (!f) { errno1 = errno; /* ENOENT can happen if the database does not exist yet. */ if (errno == ENOENT) { slog(LG_ERROR, "db-open-read: database '%s' does not yet exist; a new one will be created.", path); return NULL; } slog(LG_ERROR, "db-open-read: cannot open '%s' for reading: %s", path, strerror(errno1)); wallops(_("\2DATABASE ERROR\2: db-open-read: cannot open '%s' for reading: %s"), path, strerror(errno1)); return NULL; } rs = scalloc(sizeof(opensex_t), 1); rs->grver = 1; rs->buf = scalloc(512, 1); rs->bufsize = 512; rs->token = NULL; rs->f = f; db = scalloc(sizeof(database_handle_t), 1); db->priv = rs; db->vt = &opensex_vt; db->txn = DB_READ; db->file = sstrdup(path); db->line = 0; db->token = 0; return db; }
/** * add a bot to a channel */ void add_bot_to_chan(char *botname, char *chan) { bot *b = findbot(botname); botchan *bc = scalloc(sizeof(botchan),1); bc->next = b->chanlist; if (b->chanlist) { b->chanlist->prev = bc; } b->chanlist = bc; bc->chan = sstrdup(chan); }
static void uds_connection_cb(EV_P_ ev_io *w, int revents) { struct sockaddr_un addr; socklen_t addrlen = sizeof(addr); const int clientfd = accept(w->fd, (struct sockaddr *)&addr, &addrlen); if (clientfd == -1) { if (errno == EINTR) { return; } err(EXIT_FAILURE, "accept()"); } struct connstate *connstate = scalloc(1, sizeof(struct connstate)); ev_io *clientw = scalloc(1, sizeof(ev_io)); connstate->clientw = clientw; clientw->data = connstate; ev_io_init(clientw, read_client_setup_request_cb, clientfd, EV_READ); ev_io_start(EV_A_ clientw); }
array_list* create_array_list_size(array_list_size_t size) { array_list* list = salloc(sizeof(array_list)); if(size <= 0) size = 1; list->data = scalloc(size, sizeof(void*)); list->length = 0; list->data_size = size; return list; }
NickAlias *makenick(const char *nick) { NickAlias *na; NickCore *nc; /* First make the core */ nc = scalloc(1, sizeof(NickCore)); nc->display = sstrdup(nick); slist_init(&nc->aliases); insert_core(nc); alog("%s: group %s has been created", s_NickServ, nc->display); /* Then make the alias */ na = scalloc(1, sizeof(NickAlias)); na->nick = sstrdup(nick); na->nc = nc; slist_add(&nc->aliases, na); alpha_insert_alias(na); return na; }
/* * Build an i3String from an UTF-8 encoded string. * Returns the newly-allocated i3String. * */ i3String *i3string_from_utf8(const char *from_utf8) { i3String *str = scalloc(sizeof(i3String)); /* Get the text */ str->utf8 = sstrdup(from_utf8); /* Compute and store the length */ str->num_bytes = strlen(str->utf8); return str; }
static database_handle_t *opensex_db_open_write(const char *filename) { database_handle_t *db; opensex_t *rs; int fd; FILE *f; int errno1; char bpath[BUFSIZE], path[BUFSIZE]; snprintf(bpath, BUFSIZE, "%s/%s", datadir, filename != NULL ? filename : "services.db"); mowgli_strlcpy(path, bpath, sizeof path); mowgli_strlcat(path, ".new", sizeof path); fd = open(path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); if (fd < 0 || ! (f = fdopen(fd, "w"))) { errno1 = errno; slog(LG_ERROR, "db-open-write: cannot open '%s' for writing: %s", path, strerror(errno1)); wallops(_("\2DATABASE ERROR\2: db-open-write: cannot open '%s' for writing: %s"), path, strerror(errno1)); return NULL; } rs = scalloc(sizeof(opensex_t), 1); rs->f = f; rs->grver = 1; db = scalloc(sizeof(database_handle_t), 1); db->priv = rs; db->vt = &opensex_vt; db->txn = DB_WRITE; db->file = sstrdup(bpath); db->line = 0; db->token = 0; db_start_row(db, "GRVER"); db_write_int(db, rs->grver); db_commit_row(db); return db; }
int* intdup(int a) { int *b = (int*)scalloc(1,sizeof(int)); if(b==NULL) { printf("\nERROR: Insuficient memory to create an integer\n\n"); exit(1); } *b = a; return b; }
NickAlias *makealias(const char *nick, NickCore * nc) { NickAlias *na; /* Just need to make the alias */ na = scalloc(1, sizeof(NickAlias)); na->nick = sstrdup(nick); na->nc = nc; slist_add(&nc->aliases, na); alpha_insert_alias(na); return na; }
/** * load a bot from the database */ void load_bot(int id,char *botname, char *password, char *username, char *realname) { bot *b = scalloc(sizeof(bot),1); b->id = id; b->name = sstrdup(botname); b->password = sstrdup(password); b->realname = sstrdup(realname); b->username = sstrdup(username); b->next = botlist; if (botlist) { botlist->prev = b; } botlist = b; }
static int reply_string_cb(void *params, const unsigned char *val, size_t len) { char *str = scalloc(len + 1, 1); strncpy(str, (const char *)val, len); if (strcmp(last_key, "error") == 0) last_reply.error = str; else if (strcmp(last_key, "input") == 0) last_reply.input = str; else if (strcmp(last_key, "errorposition") == 0) last_reply.errorposition = str; else free(str); return 1; }
RD* RDCreate(int a, int dup) { RD *rdptr = (RD*)scalloc(1,sizeof(RD)); if(rdptr==NULL) { printf("\nERROR: Insuficient memory to create RD structure.\n\n"); exit(1); } rdptr->id = a; rdptr->dir = dup ? '\"': '\''; return rdptr; }