void list_prepend ( list *list, void *element ) { listNode *node = MEM_alloc ( sizeof ( listNode ) ); node->data = MEM_alloc ( list->elementSize ); memcpy ( node->data, element, list->elementSize ); node->next = list->head; list->head = node; if ( !list->tail ) { list->tail = list->head; } list->length++; }
/* * ======== BUF_create ======== */ BUF_Handle BUF_create(Uns numbuff, MEM_sizep size, Uns align, BUF_Attrs *attrs) { HeapBuf_Params params; BUF_Handle buf = NULL; Error_Block eb; if (attrs == NULL) { attrs = &BUF_ATTRS; } Error_init(&eb); HeapBuf_Params_init(¶ms); params.buf = MEM_alloc(attrs->segid, size * numbuff, align); params.numBlocks = numbuff; params.blockSize = size; params.align = align; params.bufSize = size * numbuff; if (params.buf != NULL) { buf = (BUF_Handle)HeapBuf_create(¶ms, &eb); } return (buf); }
/* * ======== mkSPipe ======== */ Static SPipeObj *mkSPipe(DEV_Handle dev) { SPipeObj *sPipe = MEM_alloc(0, sizeof (SPipeObj), 0); if (sPipe != MEM_ILLEGAL) { sPipe->dataSem = SEM_create(0, NULL); if (sPipe->dataSem == NULL) { rmSPipe(sPipe); return (MEM_ILLEGAL); } sPipe->freeSem = SEM_create(0, NULL); if (sPipe->freeSem == NULL) { rmSPipe(sPipe); return(MEM_ILLEGAL); } sPipe->readySem[DEV_INPUT] = NULL; sPipe->readySem[DEV_OUTPUT] = NULL; sPipe->device[DEV_INPUT] = NULL; sPipe->device[DEV_OUTPUT] = NULL; sPipe->id = dev->devid; return (sPipe); } else { return (MEM_ILLEGAL); } }
void smeMAP_Load(smeMap* map) { VDP_setPalette(0, map->PlaneA->Palette); VDP_loadTileSet(map->PlaneA->Tiles, TILE_USERINDEX, 0); map->PlaneA->Data = (Map*)MEM_alloc(sizeof(Map)); map->PlaneA->Data->compression = 0; map->PlaneA->Data->w = map->Width; map->PlaneA->Data->h = map->Height; map->PlaneA->Data->tilemap = map->PlaneA->Graphics; VDP_setPalette(1, map->PlaneB->Palette); VDP_loadTileSet(map->PlaneB->Tiles, TILE_USERINDEX+map->PlaneA->Tiles->numTile, 0); map->PlaneB->Data = (Map*)MEM_alloc(sizeof(Map)); map->PlaneB->Data->compression = 0; map->PlaneB->Data->w = map->Width; map->PlaneB->Data->h = map->Height; map->PlaneB->Data->tilemap = map->PlaneB->Graphics; }
// TODO:内存申请调整到片内 __inline void *HV_AllocFastMem2(int iMemSize) { void *pTemp = MEM_alloc(extHeap, iMemSize, 128); if (pTemp == MEM_ILLEGAL) { return NULL; } return pTemp; }
void list_append ( list *list, void *element ) { listNode *node = MEM_alloc ( sizeof ( listNode ) ); node->data = MEM_alloc ( list->elementSize ); node->next = NULL; memcpy ( node->data, element, list->elementSize ); if ( list->length == 0 ) { list->head = list->tail = node; } else { list->tail->next = node; list->tail = node; } list->length++; }
void TC_createCacheEx(TileCache *cache, u16 startIndex, u16 size, u16 numBloc) { cache->startIndex = startIndex; cache->limit = startIndex + size; // alloc cache structures memory cache->numBloc = numBloc; cache->blocs = MEM_alloc(numBloc * sizeof(TCBloc)); TC_clearCache(cache); }
/* * ======== DEV_mkframe ======== */ DEV_Frame *DEV_mkframe(Int segid, Uns size, Uns align) { DEV_Frame *frame; if ((frame = MEM_alloc(0, sizeof(DEV_Frame), 0)) == MEM_ILLEGAL) { return (NULL); } /* don't allocate frame buffer if size is zero */ if (size > 0) { if ((frame->addr = MEM_alloc(segid, size, align)) == MEM_ILLEGAL) { MEM_free(0, frame, sizeof(DEV_Frame)); return (NULL); } } frame->size = size; return (frame); }
static void test_mem(void) { #define MEM_COUNT 32 void *foo[MEM_COUNT]; int i, j; int oom_count; oom_count = 0; for (i = 0; i < MEM_COUNT; i++) { foo[i] = NULL; } #if 1 for (j = 0; j < 1024; j++) { i = rand() % MEM_COUNT; if (foo[i] != NULL) { MEM_free(foo[i]); } foo[i] = MEM_alloc(rand()%1000 + 10); if (foo[i] == NULL) { oom_count++; } } #else for (i = 0; i < MEM_COUNT; i++) { foo[i] = MEM_alloc(rand()%1024 + 10); } #endif for (i = 0; i < MEM_COUNT; i++) { if (foo[i] != NULL) { MEM_free(foo[i]); } } printf("Couldn't allocate %d times\n", oom_count); }
Bool bfqCreate_d( BufferQueue_Handle queue, Int BufCount, Int BufSize, Int SegID) { Int i; Ptr p; // Allocate the frames queue->Frames = MEM_alloc( 0, sizeof(BufferQueue_Frame) * BufCount, 8); queue->bDynamicFrames = TRUE; // Init the QUE and SEM objects. SEM_new(&(queue->semFullBuffers), 0); SEM_new(&(queue->semEmptyBuffers), BufCount); QUE_new( &(queue->queFullBuffers) ); QUE_new( &(queue->queEmptyBuffers) ); QUE_new( &(queue->queEmptyFrames) ); for (i=0; i<BufCount; i++) { // TODO: handle errors. // If the _BFQ_INIT_BUFFERS is defined, fill the buffers with 0xFF, // leave them uninitialized, otherwise. #ifdef _BFQ_INIT_BUFFERS p = MEM_valloc( SegID, BufSize, 0, 0xFF ); queue->Frames[i].Buffer = p; assertLog( p != MEM_ILLEGAL); #else p = MEM_alloc( SegID, BufSize, 0 ); queue->Frames[i].Buffer = p; assertLog( p != MEM_ILLEGAL); #endif QUE_put(&(queue->queEmptyBuffers), &(queue->Frames[i])); } queue->BufSize = BufSize; queue->BufCount = BufCount; queue->SegId = SegID; return TRUE; }
/* * ======== MEM_valloc ======== */ Ptr MEM_valloc(Int id, size_t size, size_t align, Char value) { Char *p; if ((p = MEM_alloc(id, size, align)) == NULL) { return (NULL); } memset(p, value, size); return (p); }
/* * ======== QUE_create ======== */ QUE_Handle QUE_create(QUE_Attrs *attrs) { QUE_Handle queue; if ((queue = MEM_alloc(0, sizeof(QUE_Elem), 0)) == MEM_ILLEGAL) { return(NULL); } QUE_new(queue); return (queue); }
void TC_init() { // not yet initialized ? if (uploads == NULL) { // alloc cache structures memory uploads = MEM_alloc(MAX_UPLOAD * sizeof(TileSet*)); // init upload uploadIndex = 0; uploadDone = FALSE; // enabled tile cache Int processing VIntProcess |= PROCESS_TILECACHE_TASK; } }
void SPR_init(u16 cacheSize) { u16 index; u16 size; // already initialized --> end it first if (SPR_isInitialized()) SPR_end(); // alloc cache structure memory VDPSpriteCache = MEM_alloc(SPRITE_CACHE_SIZE * sizeof(VDPSprite)); size = cacheSize?cacheSize:384; // get start tile index for sprite cache (reserve VRAM area just before system font) index = TILE_FONTINDEX - size; // init tile cache engine if needed TC_init(); // and create a tile cache for the sprite TC_createCache(&tcSprite, index, size); }
bool CVisBufferManager::RequestBuffer( CVisObject * pObject, Uint32 unSize, Ptr * ppBuffer, Uint32 unFlags, Int nOverlapId ) { Uint32 segId; // DEBUG:: Add a complete line if ( unSize > 1024 ) unSize += 4096; // round size up to double-word unSize = ( unSize + 7) & ~(Uint32)0x07; // Add a guardword unSize += 8; // Determine memory segment if ( ( (unFlags & BUF_FAST) != 0) || ( (unFlags & BUF_LARGE) == 0)) segId = 0; else if ( (unFlags & BUF_NON_CACHED) != 0) segId = SDRAM; else segId = SDRAM_cached; // Allocate buffer #ifdef _WINDOWS *ppBuffer = malloc(unSize); #else *ppBuffer = MEM_alloc( segId, unSize, 8); #endif // See if there was an error. if ( *ppBuffer == NULL ) { // Log the failure if ( pObject->GetClassType() == CT_PORT ) { // Get additional info if this is a port LogMsg("Alloc %dkB on %d for '%s.%s' failed", unSize/1024, segId, ((CVisPort*)pObject)->GetComponent()->GetName(), pObject->GetName()); } else { // Just write the name of the requesting object if it's not a port LogMsg("Alloc %dkB on %d for '%s' failed", unSize/1024, segId, pObject->GetName()); } return false; } // Store request infos Int i=0; while ( m_aryRequests[i].pObject != NULL) { i++; if (i==BUF_MAX_BUFFERS) return false; } m_aryRequests[i].pObject = pObject ; m_aryRequests[i].unSegId = segId; m_aryRequests[i].pBuffer = *ppBuffer; m_aryRequests[i].unSize = unSize; // Write two guard words to the end of the buffer Uint32 * p = (Uint32*)*ppBuffer; p[unSize/4-2] = BUF_GUARD; p[unSize/4-1] = BUF_GUARD; return true; }
/** * \function SendLiveStreamDataPort * \brief 使用TCP/IP协议实现H.264子码流的网络传输 * \ 默认作为服务器端,Port = 61001 * \note 除了TCP/IP的传输方式外,智能相机系统还支持H.264视频流的RTSP方式传输,其传输过程已被封装在系统内部,默认TCP/IP传输的优先级高于RTSP传输的优先级 **/ void SendLiveStreamDataPort_Another() { SOCKET sockThis, sockAccept; struct sockaddr_in addrThis, addrAccept; int nAddrLen; int nVideoWidth = 1600, nVideoHeight = 1216; int i; int isValidLink = 1; fdOpenSession( TaskSelf() ); do { g_pBuffsLiveStream_Another = MEM_alloc(extHeap, g_nMaxBuffsNumLiveStream_Another*sizeof(DataPoolItem), 256); }while(g_pBuffsLiveStream_Another == 0); memset(g_pBuffsLiveStream_Another, 0, g_nMaxBuffsNumLiveStream_Another*sizeof(DataPoolItem)); for (i = 0; i < g_nMaxBuffsNumLiveStream_Another; i++) { do { g_pBuffsLiveStream_Another[i].pBuf = MEM_alloc(extHeap, g_nMaxFrameSizeLiveStream_Another, 256); }while(g_pBuffsLiveStream_Another[i].pBuf == 0); } sockThis = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if( sockThis == INVALID_SOCKET ) { Roseek_Reset();//如果创建侦听对象失败,重启相机 } bzero( &addrAccept, sizeof(addrAccept) ); nAddrLen = sizeof(addrAccept); bzero( &addrThis, sizeof(addrThis) ); addrThis.sin_len = sizeof( addrThis ); addrThis.sin_family = AF_INET; addrThis.sin_addr.s_addr = INADDR_ANY; addrThis.sin_port = htons( 61001 ); //sockFconListen绑定 if ( bind( sockThis, (PSA) &addrThis, sizeof(addrThis) ) < 0 ) { Roseek_Reset();//如果绑定失败,重启相机 } //sockFconListen开始监听,同一时刻仅支持一个连接 if ( listen( sockThis, 1) < 0 ){ Roseek_Reset();//如果侦听失败,重启相机 } //迭代循环 while( 1 ) { sockAccept = INVALID_SOCKET; do { sockAccept = accept( sockThis, (PSA)&addrAccept, &nAddrLen ); if( sockAccept == INVALID_SOCKET) { break; } sock_setTimeout(sockAccept, SO_RCVTIMEO, 3000); sock_setTimeout(sockAccept, SO_SNDTIMEO, 3000); g_bIsLiveStreamClientConnect_Another = TRUE; g_IsUsedByTCPSend_Another = TRUE; if (SafeSend(sockAccept, (Uint8*)&nVideoWidth, 4) != 4) { break; } if (SafeSend(sockAccept, (Uint8*)&nVideoHeight, 4) != 4) { break; } while( 1 ) { if(isEncStop && g_nNumHasDataLSBuffs_Another <= 1) { Roseek_Start_Enc(); isEncStop = 0; LOG_printf( &trace, "Roseek_Start_Enc!!!"); } //等待信号灯同步 isValidLink = 1; while (!SEM_pend( &sem_LiveStreamDataReady_Another, 5000)) { int nRet; struct timeval tvTimeOut; fd_set fsVal; FD_ZERO(&fsVal); FD_SET(sockAccept, &fsVal); tvTimeOut.tv_sec = 0; tvTimeOut.tv_usec = 100; nRet = fdSelect(0, 0, &fsVal, 0, &tvTimeOut); if (0 == nRet || -1 == nRet) { isValidLink = 0; break; } } //SEM_pend( &sem_LiveStreamDataReady_Another, SYS_FOREVER ); if (!isValidLink) { break; } //判断上传哪个缓冲区内容 if (g_nNumHasDataLSBuffs_Another <= 0) { continue; } if (!isEncStop && (g_nNumHasDataLSBuffs_Another >= (g_nMaxBuffsNumLiveStream_Another-3))) { Roseek_Stop_Enc(); isEncStop = 1; LOG_printf( &trace, "Roseek_Stop_Enc!!!"); } if (g_nNumHasDataLSBuffs_Another >= (g_nMaxBuffsNumLiveStream_Another-1) && (g_pBuffsLiveStream_Another[g_nReadPosLSBuffs_Another].pBuf[8]&0x1F) != 7) { LOG_printf( &trace, "this frame not send!!!"); } else { nDataLen = *(Uint32*)(g_pBuffsLiveStream_Another[g_nReadPosLSBuffs_Another].pBuf) + 4; if((nSendlen = SafeSend( sockAccept, g_pBuffsLiveStream_Another[g_nReadPosLSBuffs_Another].pBuf, nDataLen)) != nDataLen) { LOG_printf( &trace, "send error!!!"); break; } } g_nReadPosLSBuffs_Another = (++g_nReadPosLSBuffs_Another)%g_nMaxBuffsNumLiveStream_Another; g_nNumHasDataLSBuffs_Another--; } }while(0); if (sockAccept != INVALID_SOCKET) { fdClose(sockAccept); sockAccept = INVALID_SOCKET; } g_IsUsedByTCPSend_Another = FALSE; g_bIsLiveStreamClientConnect_Another = FALSE; SEM_reset( &sem_LiveStreamDataReady_Another, 0 ); g_nNumHasDataLSBuffs_Another = 0; g_nReadPosLSBuffs_Another = 0; g_nWritePosLSBuffs_Another = 0; if(isEncStop) { Roseek_Start_Enc(); isEncStop = 0; LOG_printf( &trace, "Roseek_Start_Enc!!!"); } }//迭代循环 }
/* * ======== DPI_open ======== */ Static Int DPI_open(DEV_Handle dev, String name) { PipeObj *pipe; SPipeObj *sPipe, *tmpPipe; /* decode and validate devid */ if (dev->devid < 0) { dev->devid = atoi(name); } SEM_pend(mutex, SYS_FOREVER); /* search pipe list for previously opened pipe with same id */ sPipe = MEM_ILLEGAL; if (!QUE_empty(sPipeList)) { tmpPipe = (SPipeObj *)QUE_head(sPipeList); do { if (tmpPipe->id == dev->devid) { sPipe = tmpPipe; break; } tmpPipe = (SPipeObj *)QUE_next((&tmpPipe->link)); } while (tmpPipe != (SPipeObj *)sPipeList); } if (sPipe == MEM_ILLEGAL) { /* * Allocate and initialize SPipeObj on first open. */ sPipe = mkSPipe(dev); if (sPipe == MEM_ILLEGAL) { SEM_post(mutex); return SYS_EALLOC; } QUE_put(sPipeList, &sPipe->link); } else { /* sPipe found on list */ if (sPipe->device[dev->mode] != NULL) { /* * Only one input and one output allowed */ SEM_post(mutex); return SYS_EBUSY; } } sPipe->device[dev->mode] = dev; SEM_post(mutex); pipe = MEM_alloc(0, sizeof (PipeObj), 0); if (pipe == MEM_ILLEGAL) { /* * We need to undo work done by mkSPipe() if first open. * Also need to undo changes to sPipeList queue. */ QUE_remove(&sPipe->link); rmSPipe(sPipe); return SYS_EALLOC; } /* * Criss-cross SEM handles so both sides are referencing * the same physical objects. */ if (dev->mode == DEV_INPUT) { pipe->fromSem = sPipe->dataSem; pipe->toSem = sPipe->freeSem; } else { pipe->toSem = sPipe->dataSem; pipe->fromSem = sPipe->freeSem; } /* * Point things around. */ pipe->sPipe = sPipe; dev->object = (Ptr)pipe; return (SYS_OK); }
/* * ======== DOV_open ======== */ static Int DOV_open(DEV_Handle device, String name) { DOV_CopyObj *copy; DEV_Device *entry; Int status = SYS_EALLOC; DEV_Frame *frame; size_t size; if (device->mode != DEV_INPUT) { return (SYS_EINVAL); } /* * If devid is nonzero, it holds the 'size' of the overlap buffer. */ if (device->devid > 0) { size = device->devid; } else { size = atoi(name); /* * Skip the numeric characters to get to the underlying * device's name. */ while (isdigit(*name)) { name++; } } if (size <= 0 || size >= device->bufsize) { return (SYS_EINVAL); } /* * find underlying device in device table */ name = DEV_match(name, &entry); if (entry == NULL) { return (SYS_ENODEV); } /* allocate copy object */ if ((copy = MEM_alloc(0, sizeof(DOV_CopyObj), 0)) == MEM_ILLEGAL) { return (SYS_EALLOC); } copy->size = size; /* allocate and initialize overlap buffer */ if ((copy->overlap = MEM_valloc(0, size, 0, DOV->INITIAL)) == MEM_ILLEGAL) { goto e1; } copy->dobj = *device; /* copy descriptor fields */ copy->dobj.fxns = *(DEV_Fxns *)(entry->fxns); copy->dobj.devid = entry->devid; copy->dobj.params = entry->params; /* size of underlying buffers */ copy->dobj.bufsize = device->bufsize - size; /* * create queues and frames for underlying device. */ if ((copy->dobj.todevice = QUE_create(NULL)) == NULL) { goto e2; } if ((copy->dobj.fromdevice = QUE_create(NULL)) == NULL) { goto e3; } /* * adjust frame size and address according to the overlap size before * copying frames to underlying device's 'todevice' queue */ while (!QUE_empty(device->todevice)) { frame = QUE_get(device->todevice); frame->size = frame->size - size; frame->addr = (Char *)frame->addr + size; QUE_put(copy->dobj.todevice, frame); } /* open underlying device */ if ((status = DEV_open((©->dobj), name)) != SYS_OK) { goto e4; } device->object = (Ptr)copy; return (SYS_OK); /* all is well */ /* free memory and return error code */ e4: QUE_delete(copy->dobj.fromdevice); e3: QUE_delete(copy->dobj.todevice); e2: MEM_free(0, copy->overlap, copy->size); e1: MEM_free(0, copy, sizeof(DOV_CopyObj)); return (status); }