/* Allocate a new buffer and put it at the end of the chain of buffers * scheduled for output. Return 1 if we have more bytes in buffers * than allowed afterwards. */ static INLINE int append_buffer(struct pike_string *s) /* 1=buffer full */ { struct buffer *b; debug_malloc_touch(s); if(THIS->fd!= -1) { fd_lseek(THIS->fd, THIS->pos, SEEK_SET); fd_write(THIS->fd, s->str, s->len); THIS->pos+=s->len; return 0; } else { nbuffers++; b=ALLOC_STRUCT(buffer); b->next=NULL; b->s=s; sbuffers += s->len; add_ref(s); if (THIS->lastbuffer) THIS->lastbuffer->next=b; else THIS->firstbuffer=b; THIS->lastbuffer=b; THIS->bytes_in_buffer+=s->len; } return THIS->bytes_in_buffer > MAX_BYTES_IN_BUFFER; }
static llthread_child_t *llthread_child_new() { llthread_child_t *this = ALLOC_STRUCT(llthread_child_t); if(!this) return NULL; memset(this, 0, sizeof(llthread_child_t)); /* create new lua_State for the thread. */ /* open standard libraries. */ this->L = luaL_newstate(); open_thread_libs(this->L); return this; }
/* Allocate a new struct input, link it last in the linked list */ static INLINE struct input *new_input(void) { struct input *i; ninputs++; i=ALLOC_STRUCT(input); i->type=I_NONE; i->next=NULL; if (THIS->lastinput) THIS->lastinput->next=i; else THIS->firstinput=i; THIS->lastinput=i; return i; }
int main(void) { //check_offloading(); S s1; ALLOC_STRUCT(s1); #if MAP_ALL printf("Map all\n"); INIT_STRUCT(s1); print(&s1); TEST_MAP( INIT_STRUCT(s1), _Pragma("omp target map(s1)"), { s1.a++; s1.c++; s1.e++; s1.x++; },
struct instr_counter *init_instr_storage_pointers(int depth) { int e; struct instr_counter *d; if(!depth) return 0; d=ALLOC_STRUCT(instr_counter); if(!d) { fprintf(stderr,"-p%d: out of memory.\n",p_flag); exit(2); } dmalloc_accept_leak(d); d->runned=0; for(e=0;e<F_MAX_OPCODE-F_OFFSET;e++) d->next[e]=init_instr_storage_pointers(depth-1); return d; }
/*! @decl program load_module(string module_name) *! *! Load a binary module. *! *! This function loads a module written in C or some other language *! into Pike. The module is initialized and any programs or constants *! defined will immediately be available. *! *! When a module is loaded the C function @tt{pike_module_init()@} will *! be called to initialize it. When Pike exits @tt{pike_module_exit()@} *! will be called. These two functions @b{must@} be available in the module. *! *! @note *! The current working directory is normally not searched for *! dynamic modules. Please use @expr{"./name.so"@} instead of just *! @expr{"name.so"@} to load modules from the current directory. */ void f_load_module(INT32 args) { extern int global_callable_flags; void *module; modfun init, exit; struct module_list *new_module; struct pike_string *module_name; ONERROR err; module_name = Pike_sp[-args].u.string; if((Pike_sp[-args].type != T_STRING) || (module_name->size_shift) || string_has_null(module_name)) { Pike_error("Bad argument 1 to load_module()\n"); } { struct module_list *mp; for (mp = dynamic_module_list; mp; mp = mp->next) if (mp->name == module_name && mp->module_prog) { pop_n_elems(args); ref_push_program(mp->module_prog); return; } } /* Removing RTLD_GLOBAL breaks some PiGTK themes - Hubbe */ /* Using RTLD_LAZY is faster, but makes it impossible to * detect linking problems at runtime.. */ module=dlopen(module_name->str, RTLD_NOW /*|RTLD_GLOBAL*/ ); if(!module) { struct object *err_obj = low_clone (module_load_error_program); #define LOADERR_STRUCT(OBJ) \ ((struct module_load_error_struct *) (err_obj->storage + module_load_error_offset)) const char *err = dlerror(); if (err) { if (err[strlen (err) - 1] == '\n') push_string (make_shared_binary_string (err, strlen (err) - 1)); else push_text (err); } else push_constant_text ("Unknown reason"); add_ref (LOADERR_STRUCT (err_obj)->path = Pike_sp[-args - 1].u.string); add_ref (LOADERR_STRUCT (err_obj)->reason = Pike_sp[-1].u.string); if (Pike_sp[-args].u.string->len < 1024) { throw_error_object (err_obj, "load_module", Pike_sp - args - 1, args, "load_module(\"%s\") failed: %s\n", module_name->str, Pike_sp[-1].u.string->str); } else { throw_error_object (err_obj, "load_module", Pike_sp - args - 1, args, "load_module() failed: %s\n", Pike_sp[-1].u.string->str); } } #ifdef PIKE_DEBUG { struct module_list *mp; for (mp = dynamic_module_list; mp; mp = mp->next) if (mp->module == module && mp->module_prog) { fprintf(stderr, "load_module(): Module loaded twice:\n" "Old name: %s\n" "New name: %s\n", mp->name->str, module_name->str); pop_n_elems(args); ref_push_program(mp->module_prog); return; } } #endif /* PIKE_DEBUG */ init = CAST_TO_FUN(dlsym(module, "pike_module_init")); if (!init) { init = CAST_TO_FUN(dlsym(module, "_pike_module_init")); if (!init) { dlclose(module); Pike_error("pike_module_init missing in dynamic module \"%S\".\n", module_name); } } exit = CAST_TO_FUN(dlsym(module, "pike_module_exit")); if (!exit) { exit = CAST_TO_FUN(dlsym(module, "_pike_module_exit")); if (!exit) { dlclose(module); Pike_error("pike_module_exit missing in dynamic module \"%S\".\n", module_name); } } #if defined(__NT__) && defined(_M_IA64) { fprintf(stderr, "pike_module_init: 0x%p\n" " func: 0x%p\n" " gp: 0x%p\n", init, ((void **)init)[0], ((void **)init)[1]); fprintf(stderr, "pike_module_exit: 0x%p\n" " func: 0x%p\n" " gp: 0x%p\n", exit, ((void **)exit)[0], ((void **)exit)[1]); } #endif /* __NT__ && _M_IA64 */ new_module=ALLOC_STRUCT(module_list); new_module->next=dynamic_module_list; dynamic_module_list=new_module; new_module->module=module; copy_shared_string(new_module->name, Pike_sp[-args].u.string); new_module->module_prog = NULL; new_module->init=init; new_module->exit=exit; enter_compiler(new_module->name, 1); start_new_program(); global_callable_flags|=CALLABLE_DYNAMIC; #ifdef PIKE_DEBUG { struct svalue *save_sp=Pike_sp; #endif SET_ONERROR(err, cleanup_compilation, NULL); #if defined(__NT__) && defined(_M_IA64) fprintf(stderr, "Calling pike_module_init()...\n"); #endif /* __NT__ && _M_IA64 */ (*(modfun)init)(); #if defined(__NT__) && defined(_M_IA64) fprintf(stderr, "pike_module_init() done.\n"); #endif /* __NT__ && _M_IA64 */ UNSET_ONERROR(err); #ifdef PIKE_DEBUG if(Pike_sp != save_sp) Pike_fatal("load_module(%s) left %ld droppings on stack!\n", module_name->str, PTRDIFF_T_TO_LONG(Pike_sp - save_sp)); } #endif pop_n_elems(args); { struct program *p = end_program(); exit_compiler(); if (p) { if ( #if 0 p->num_identifier_references #else /* !0 */ 1 #endif /* 0 */ ) { push_program(p); add_ref(new_module->module_prog = Pike_sp[-1].u.program); } else { /* No identifier references -- Disabled module. */ free_program(p); push_undefined(); } } else { /* Initialization failed. */ new_module->exit(); dlclose(module); dynamic_module_list = new_module->next; free_string(new_module->name); free(new_module); Pike_error("Failed to initialize dynamic module \"%S\".\n", module_name); } } }
/** ***************************************************************************** * @ingroup dsaPerformance * dsaPerform * * @description * This function generates all the DSA parameters required to perform a DSA * sign and DSA verify operation. A user defined number of random messages * are generated and signed, then the signature is verified * *****************************************************************************/ CpaStatus dsaPerform(dsa_test_params_t* setup) { Cpa32U i=0; Cpa32U outerLoop = 0; CpaBoolean verifyStatus = CPA_TRUE; CpaStatus status = CPA_STATUS_SUCCESS; /*DSA parameters */ /*DSA Q parameter, this shall be populated by the hard coded Q at the top * of this file */ CpaFlatBuffer dsaQ = {0}; /*random number X used to generate Y and Sign R&S */ CpaFlatBuffer* dsaX = NULL; /*DSA P parameter, this shall be populated by the hard coded P at the top * of this file */ CpaFlatBuffer dsaP = {0}; /*H is used to generate G, H is hard coded to DEFAULT_H_VALUE */ CpaFlatBuffer dsaH = {0}; /* DSA G parameter used to generate Y, the signature R&S, and to verify */ CpaFlatBuffer dsaG = {0}; /*DSA Y parameter is used in the verification stage */ CpaFlatBuffer* dsaY = NULL; /*K is a random number used in the generation of signature R&S */ CpaFlatBuffer* dsaK = NULL; /*M is the message to be signed */ CpaFlatBuffer* dsaM = NULL; /*R&S is used to store the DSA Signature */ CpaFlatBuffer* dsaR = NULL; CpaFlatBuffer* dsaS = NULL; /*Z is the digest of the message in dsaM */ CpaFlatBuffer* dsaZ = NULL; perf_data_t* pDsaData = NULL; /*GCC compiler complains without the double {{}} to init the following * structures*/ CpaCyDsaGParamGenOpData gOpData = {{0, NULL}, {0, NULL}, {0, NULL}}; CpaCyDsaYParamGenOpData yOpData = {{0}}; CpaCyDsaRSSignOpData rsOpData = {{0}}; CpaCyDsaVerifyOpData* verifyOpData = NULL; Cpa8U* pDataPtr = NULL; Cpa32U sizeOfp = 0; Cpa8U* qDataPtr = NULL; Cpa32U sizeOfq = 0; Cpa32U node = 0; CpaCyDsaVerifyCbFunc cbFunc = NULL; status = sampleCodeCyGetNode(setup->cyInstanceHandle, &node); if(CPA_STATUS_SUCCESS != status) { PRINT_ERR("Could not determibne node for memory allocation\n"); return status; } pDsaData = setup->performanceStats; pDsaData->threadReturnStatus = CPA_STATUS_FAIL; pDsaData->numOperations = (Cpa64U)setup->numBuffers * setup->numLoops; /*check the p and q input len and set the pointers to the data */ if(MODULUS_1024_BIT / NUM_BITS_IN_BYTE == setup->pLenInBytes && EXPONENT_160_BIT / NUM_BITS_IN_BYTE == setup->qLenInBytes) { pDataPtr = dsa_1024_160_p; qDataPtr = dsa_1024_160_q; sizeOfp = sizeof(dsa_1024_160_p); sizeOfq = sizeof(dsa_1024_160_q); } else if(MODULUS_2048_BIT / NUM_BITS_IN_BYTE == setup->pLenInBytes && EXPONENT_224_BIT / NUM_BITS_IN_BYTE == setup->qLenInBytes) { pDataPtr = dsa_2048_224_p; qDataPtr = dsa_2048_224_q; sizeOfp = sizeof(dsa_2048_224_p); sizeOfq = sizeof(dsa_2048_224_q); } else if(MODULUS_2048_BIT / NUM_BITS_IN_BYTE == setup->pLenInBytes && EXPONENT_256_BIT / NUM_BITS_IN_BYTE == setup->qLenInBytes) { pDataPtr = dsa_2048_256_p; qDataPtr = dsa_2048_256_q; sizeOfp = sizeof(dsa_2048_256_p); sizeOfq = sizeof(dsa_2048_256_q); } else if(MODULUS_3072_BIT / NUM_BITS_IN_BYTE == setup->pLenInBytes && EXPONENT_256_BIT / NUM_BITS_IN_BYTE == setup->qLenInBytes) { pDataPtr = dsa_3072_256_p; qDataPtr = dsa_3072_256_q; sizeOfp = sizeof(dsa_3072_256_p); sizeOfq = sizeof(dsa_3072_256_q); } else { PRINT_ERR("P & Q len not supported\n"); /*thread status is init to fail so just reutrn fail here*/ return CPA_STATUS_FAIL; } /* Completion used in callback */ sampleCodeSemaphoreInit(&pDsaData->comp, 0); #define ALLOC_STRUCT(ptr, size) \ do{ \ ptr = qaeMemAlloc(size * setup->numBuffers); \ if(NULL == ptr) \ { \ PRINT_ERR("Could not allocate memory\n"); \ FREE_DSA_MEM; \ return CPA_STATUS_FAIL; \ } \ memset(ptr,0,size * setup->numBuffers); \ }while(0) /*Allocate all the buffers */ ALLOC_STRUCT(dsaX, sizeof(CpaFlatBuffer)); ALLOC_STRUCT(dsaY, sizeof(CpaFlatBuffer)); ALLOC_STRUCT(dsaK, sizeof(CpaFlatBuffer)); ALLOC_STRUCT(dsaM, sizeof(CpaFlatBuffer)); ALLOC_STRUCT(dsaR, sizeof(CpaFlatBuffer)); ALLOC_STRUCT(dsaS, sizeof(CpaFlatBuffer)); ALLOC_STRUCT(dsaZ, sizeof(CpaFlatBuffer)); ALLOC_STRUCT(verifyOpData, sizeof(CpaCyDsaVerifyOpData)); /************************************************************************ * STAGE 1 Setup up the DSA parameters, generate X, G, Y, K, Z, * generate user defined number of messages to be signed * calculate the digest of the messages * sign all the messages * setup the verification data structure **************************************************************************/ /*set Q */ ALLOC_FLAT_BUFF_DATA(setup->cyInstanceHandle, &dsaQ, setup->qLenInBytes, qDataPtr, sizeOfq, FREE_DSA_MEM); /*generate X for each buffer */ for(i=0; i<setup->numBuffers; i++) { /*Choose X is generated by random method, where 0 < X < Q */ ALLOC_FLAT_BUFF_DATA(setup->cyInstanceHandle, &dsaX[i], setup->qLenInBytes, NULL, 0, FREE_DSA_MEM); dsaGenRandom(&dsaX[i], &dsaQ); } /*set P */ ALLOC_FLAT_BUFF_DATA(setup->cyInstanceHandle, &dsaP, setup->pLenInBytes, pDataPtr, sizeOfp, FREE_DSA_MEM); /*************************************************************************** * set genG opData and generate G * ************************************************************************/ /*H is required to genG */ ALLOC_FLAT_BUFF_DATA(setup->cyInstanceHandle, &dsaH, setup->pLenInBytes, NULL, 0, FREE_DSA_MEM); memset(dsaH.pData, 0, dsaH.dataLenInBytes); dsaH.pData[setup->pLenInBytes-1] = DEFAULT_H_VALUE; /*allocate space for G */ ALLOC_FLAT_BUFF_DATA(setup->cyInstanceHandle, &dsaG, setup->pLenInBytes, NULL, 0, FREE_DSA_MEM); /*set opData to generate G */ gOpData.P.pData = dsaP.pData; gOpData.P.dataLenInBytes = dsaP.dataLenInBytes; gOpData.Q.pData = dsaQ.pData; gOpData.Q.dataLenInBytes = dsaQ.dataLenInBytes; gOpData.H.pData = dsaH.pData; gOpData.H.dataLenInBytes = dsaH.dataLenInBytes; status = dsaGenG(setup->cyInstanceHandle, &gOpData, &dsaG); if(CPA_STATUS_SUCCESS != status) { PRINT_ERR("Failed to generate DSA parameter G\n"); FREE_DSA_MEM; return CPA_STATUS_FAIL; } /*generate a Y for each buffer */ for(i=0;i<setup->numBuffers;i++) { /*set the opData to gen Y */ ALLOC_FLAT_BUFF_DATA(setup->cyInstanceHandle, &dsaY[i], setup->pLenInBytes, NULL, 0, FREE_DSA_MEM); yOpData.P.pData = dsaP.pData; yOpData.P.dataLenInBytes = dsaP.dataLenInBytes; yOpData.G.pData = dsaG.pData; yOpData.G.dataLenInBytes = dsaG.dataLenInBytes; yOpData.X.pData = dsaX[i].pData; yOpData.X.dataLenInBytes = dsaX[i].dataLenInBytes; status = dsaGenY(setup->cyInstanceHandle,&yOpData,&dsaY[i]); if(CPA_STATUS_SUCCESS != status) { PRINT_ERR("Error Generating Y for buffer %d\n", i); /*free all the pData buffers allocated and Array of pointers * allocated */ FREE_DSA_MEM; return CPA_STATUS_FAIL; } /*Generate a random per-message value K, where 0 < K < Q. */ ALLOC_FLAT_BUFF_DATA(setup->cyInstanceHandle, &dsaK[i], setup->qLenInBytes, NULL, 0, FREE_DSA_MEM); dsaGenRandom(&dsaK[i], &dsaQ); /*generate a message to sign */ /*allocate space for message */ ALLOC_FLAT_BUFF_DATA(setup->cyInstanceHandle, &dsaM[i], setup->pLenInBytes, NULL, 0, FREE_DSA_MEM); /*allocate space for digest of message */ ALLOC_FLAT_BUFF_DATA(setup->cyInstanceHandle, &dsaZ[i], setup->qLenInBytes, NULL, 0, FREE_DSA_MEM); /*generate random message */ generateRandomData(dsaM[i].pData, dsaM[i].dataLenInBytes); /*calculate digest of message */ status = dsaGenZ(setup->cyInstanceHandle, &dsaM[i], setup->hashAlg, &dsaZ[i]); if(CPA_STATUS_SUCCESS != status) { PRINT_ERR("Error Generating Z for buffer %d\n", i); FREE_DSA_MEM; return CPA_STATUS_FAIL; } /*Gen R & S signature */ rsOpData.G.pData = dsaG.pData; rsOpData.G.dataLenInBytes = dsaG.dataLenInBytes; rsOpData.K.pData = dsaK[i].pData; rsOpData.K.dataLenInBytes = dsaK[i].dataLenInBytes; rsOpData.P.pData = dsaP.pData; rsOpData.P.dataLenInBytes = dsaP.dataLenInBytes; rsOpData.Q.pData = dsaQ.pData; rsOpData.Q.dataLenInBytes = dsaQ.dataLenInBytes; rsOpData.X.pData = dsaX[i].pData; rsOpData.X.dataLenInBytes = dsaX[i].dataLenInBytes; rsOpData.Z.pData = dsaZ[i].pData; rsOpData.Z.dataLenInBytes = dsaZ[i].dataLenInBytes; ALLOC_FLAT_BUFF_DATA(setup->cyInstanceHandle, &dsaR[i], setup->qLenInBytes, NULL, 0, FREE_DSA_MEM); ALLOC_FLAT_BUFF_DATA(setup->cyInstanceHandle, &dsaS[i], setup->qLenInBytes, NULL, 0, FREE_DSA_MEM); status = dsaGenRS(setup->cyInstanceHandle, &rsOpData, &dsaR[i], &dsaS[i]); if(CPA_STATUS_SUCCESS != status) { PRINT_ERR("Error Generating R&S for buffer %d\n", i); FREE_DSA_MEM; return CPA_STATUS_FAIL; } /*Verify signature */ verifyOpData[i].P.pData = dsaP.pData; verifyOpData[i].P.dataLenInBytes = dsaP.dataLenInBytes; verifyOpData[i].Q.pData = dsaQ.pData; verifyOpData[i].Q.dataLenInBytes = dsaQ.dataLenInBytes; verifyOpData[i].G.pData= dsaG.pData; verifyOpData[i].G.dataLenInBytes = dsaG.dataLenInBytes; verifyOpData[i].Y.pData= dsaY[i].pData; verifyOpData[i].Y.dataLenInBytes = dsaY[i].dataLenInBytes; verifyOpData[i].Z.pData= dsaZ[i].pData; verifyOpData[i].Z.dataLenInBytes = dsaZ[i].dataLenInBytes; verifyOpData[i].R.pData= dsaR[i].pData; verifyOpData[i].R.dataLenInBytes = dsaR[i].dataLenInBytes; verifyOpData[i].S.pData= dsaS[i].pData; verifyOpData[i].S.dataLenInBytes = dsaS[i].dataLenInBytes; } /*set the callback function if asynchronous mode is set*/ if(ASYNC == setup->syncMode) { cbFunc = dsaVerifyCb; } /************************************************************************ * STAGE 2 repeatedly verify all the signatures and measure the performance ************************************************************************* */ /*this barrier will wait until all threads get to this point */ sampleCodeBarrier(); /* get a timestamp before submitting any requests. After submitting * all requests a final timestamp is taken in the callback function. * These two times and the number of requests submitted are used to * calculate operations per second */ pDsaData->startCyclesTimestamp = sampleCodeTimestamp(); for(outerLoop = 0; outerLoop<setup->numLoops; outerLoop++) { for(i=0;i<setup->numBuffers;i++) { do { status = cpaCyDsaVerify(setup->cyInstanceHandle, cbFunc, pDsaData, &verifyOpData[i], &verifyStatus); if(CPA_STATUS_RETRY == status) { pDsaData->retries++; /*once we get to many retries, perform a context switch * to give the acceleration engine a small break */ if(RETRY_LIMIT == (pDsaData->retries % (RETRY_LIMIT+1))) { AVOID_SOFTLOCKUP; } } } while (CPA_STATUS_RETRY == status); /*if for some reason the DSA verify returns fail, decrease the * numOperations expected in the callback so that the code does not * wait forever */ if(CPA_STATUS_SUCCESS != status) { PRINT_ERR("DSA Verify function failed with status:%d\n", status); break; } } if(CPA_STATUS_SUCCESS != status) { break; } } if (CPA_STATUS_SUCCESS == status) { status = waitForResponses(pDsaData, setup->syncMode, setup->numBuffers, setup->numLoops); } /*free Arrays of buffer pointers and pData */ FREE_DSA_MEM; sampleCodeSemaphoreDestroy(&pDsaData->comp); pDsaData->threadReturnStatus = status; if(CPA_STATUS_SUCCESS != setup->performanceStats->threadReturnStatus) { status = CPA_STATUS_FAIL; } return status; }