struct PitchHandler_context* PitchHandler_init(struct Fret_context* fctx,void* (*allocFn)(unsigned long),int (*fail)(const char*,...),int (*logger)(const char*,...)) { struct PitchHandler_context* ctx = (struct PitchHandler_context*)allocFn(sizeof(struct PitchHandler_context)); ctx->fret = fctx; ctx->tuneSpeed = 0.1; ctx->rowCount = 3; ctx->colCount = 5; ctx->noteDiff = 48; ctx->fretiterator = 0; ctx->fretoffsetX = 0; ctx->fretoffsetY = 0; ctx->fretoffsetXInitial = 0.5; ctx->fretoffsetYInitial = 0.5; ctx->lastFingerDown = NOBODY; ctx->lastNoteDown = 0; ctx->noteDiffOurs = 0; ctx->doOctaveRounding = 1; ctx->fail = fail; ctx->logger = logger; ctx->initSnap = 1; for(int i=0; i<FINGERMAX; i++) { ctx->fingers[i].isActive = 0; ctx->fingers[i].velocity = 0; PitchHandler_setTuneInterval(ctx, i, 5); } return ctx; }
/** @memo Allocate memory. @doc Allocates, if possible, a portion of memory of 'size' bytes. @precondition <b>Only use if dynamic memory is required. MUST</b> not be called directly. Use the <b>rtp_malloc</b> macro defined in rtpmem.h and turn on RTP_TRACK_LOCAL_MEMORY. If the preprocessor macros used are unavailable in the target environment, define them at the compiler to a chosen dummy value. @return Pointer to the location of the allocated spcae, 0 otherwise. For debugging purposes; if the cause of the error is obtainable at the native Memory System layer, turn on RTP_DEBUG in rtpmem.c to display the native error value. */ void * _rtp_debug_malloc ( unsigned long size, /** Number of bytes to allocate. */ RTP_MALLOC_FN allocFn, /** Memory allocation function pointer. */ const char *file, /** The preprocessor macro __FILE__. */ long line_num, /** The preprocessor macro __LINE__. */ const char *comment, unsigned long flags ) { void * ptr; if (!size) { return ((void *)0); } #if (RTP_MEM_RESTRAIN) /* ----------------------------------- */ /* when we have more than giMemBase */ /* used, randomly deny malloc */ /* requests for torture purposes */ /* ----------------------------------- */ if ((size + (unsigned long) giMemUsed > giMemLimit) || ((giMemUsed > (long) giMemBase) && (rtp_rand() % 1000 < gcMallocFailRate))) { giMemClipped++; return ((void *)0); } #endif ptr = allocFn (size + RTP_MEM_BUFFER_GUARD + RTP_MEM_BUFFER_PREPAD + sizeof(RTPMemBlockHeader)); if (ptr) { _rtp_AddMemBlock (ptr, size, file, line_num, comment, flags); ptr = (void *)(((unsigned char *)ptr) + sizeof(RTPMemBlockHeader)); } if (!ptr) { return ((void *)0); } #if (RTP_MEM_BUFFER_PREPAD > 0) rtp_memset (ptr, RTP_MEM_PREPAD_BYTE, RTP_MEM_BUFFER_PREPAD); ptr = (void *)(((unsigned char *)ptr) + RTP_MEM_BUFFER_PREPAD); #endif #if (RTP_MEM_BUFFER_GAURD > 0) rtp_memset ((void *)(((unsigned char *)ptr) + size), RTP_MEM_GUARD_BYTE, RTP_MEM_BUFFER_GUARD); #endif return (ptr); }
Subschema *newSubschema() { void *ptr = allocFn(sizeof(Subschema)); if (!ptr) { throw std::runtime_error( "Failed to allocate memory for shared empty sub-schema"); } try { return new (ptr) Subschema(); } catch (...) { freeFn(ptr); throw; } }
/*************************************************************************** * Function : SkipListFindOrInsert() * Description : Searches for the specified data item * If found returns it, otherwise allocates one using * user specified function, sets key and data and then * returns it. * If no user specified function exists, the given key * and data are inserted. * * * Input : The list in which the element is to be inserted, key, * element to be inserted, a allocation function if new key and * data are to be allocated, a return value for a flag to indicate * if allocation was called. * Output : sets found if it found an element. * Returns : Node ptr on success, NULL on failiure * ***************************************************************************/ SkipListNode_t * SkipListFindOrInsert( SkipList_t *list, void *key, void *value , SkipListUserAlloc_t allocFn, int *found ) { int i = 0; int newLevel = 0; SkipListNode_t *x= NULL; SkipListNode_t *update[SKIPLIST_MAXLEVEL]; void *newKey; void *newValue; *found = 0; /* * Scan all levels while (list-key<serach-key) starting * with header in its level. */ for ( x=list->header, i=list->level; i >= 0; i-- ) { while ( x->forward[i] != 0 && list->cmpFn(x->forward[i]->key, key) < 0 ) { x = x->forward[i]; } /* * Save level pointer. */ update[i] = x; } x = x->forward[0]; /* * Element already exists. */ if ( x && list->cmpFn(x->key, key) == 0 ) { *found = 1; return x; } /* * Put a new element in the list for this key. * Get the new level and fix the list level. */ newLevel = getNewLevel( list->maxLevels ); /* * Adjust the header level. */ if ( newLevel > list->level ) { for ( i=list->level+1; i<=newLevel; i++ ) { update[i] = list->header; } list->level = newLevel; } if ( allocFn ) { /* * User has a callback function to allocate newKey and newValue */ if ( !allocFn(key,value,&newKey,&newValue) ) return NULL; } else { /* * User intends to insert the supplied key and value */ newKey = key; newValue = value; } /* * Make the new element and insert it in the list. */ x = newSkipListNode( list, newKey, newValue, newLevel ); if (!x) return NULL; /* * Scan all levels. */ for ( i=0; i<=newLevel; i++ ) { /* * Set the next pointer of the new element. */ x->forward[i] = update[i]->forward[i]; update[i]->forward[i] = x; } list->numNodes++; return x; }