BSOCK_HANDLE BSckAttach(SYS_SOCKET SockFD, int iBufferSize) { BuffSocketData *pBSD = (BuffSocketData *) SysAlloc(sizeof(BuffSocketData)); if (pBSD == NULL) return INVALID_BSOCK_HANDLE; char *pszBuffer = (char *) SysAlloc(iBufferSize); if (pszBuffer == NULL) { SysFree(pBSD); return INVALID_BSOCK_HANDLE; } pBSD->SockFD = SockFD; pBSD->iBufferSize = iBufferSize; pBSD->pszBuffer = pszBuffer; pBSD->iBytesInBuffer = 0; pBSD->iReadIndex = 0; pBSD->IOops.pPrivate.handle = SockFD; pBSD->IOops.pName = BSckSock_Name; pBSD->IOops.pFree = BSckSock_Free; pBSD->IOops.pRead = BSckSock_Read; pBSD->IOops.pWrite = BSckSock_Write; pBSD->IOops.pSendFile = BSckSock_SendFile; return (BSOCK_HANDLE) pBSD; }
// Try to add at least npage pages of memory to the heap, // returning whether it worked. static bool MHeap_Grow(MHeap *h, uintptr npage) { uintptr ask; void *v; MSpan *s; // Ask for a big chunk, to reduce the number of mappings // the operating system needs to track; also amortizes // the overhead of an operating system mapping. // For Native Client, allocate a multiple of 64kB (16 pages). npage = (npage+15)&~15; ask = npage<<PageShift; if(ask < HeapAllocChunk) ask = HeapAllocChunk; v = SysAlloc(ask); if(v == nil) { if(ask > (npage<<PageShift)) { ask = npage<<PageShift; v = SysAlloc(ask); } if(v == nil) return false; } mstats.heap_sys += ask; if((byte*)v < h->min || h->min == nil) h->min = v; if((byte*)v+ask > h->max) h->max = (byte*)v+ask; // NOTE(rsc): In tcmalloc, if we've accumulated enough // system allocations, the heap map gets entirely allocated // in 32-bit mode. (In 64-bit mode that's not practical.) if(!MHeapMap_Preallocate(&h->map, ((uintptr)v>>PageShift) - 1, (ask>>PageShift) + 2)) { SysFree(v, ask); return false; } // Create a fake "in use" span and free it, so that the // right coalescing happens. s = FixAlloc_Alloc(&h->spanalloc); mstats.mspan_inuse = h->spanalloc.inuse; mstats.mspan_sys = h->spanalloc.sys; MSpan_Init(s, (uintptr)v>>PageShift, ask>>PageShift); MHeapMap_Set(&h->map, s->start, s); MHeapMap_Set(&h->map, s->start + s->npages - 1, s); s->state = MSpanInUse; MHeap_FreeLocked(h, s); return true; }
// stackcacherefill/stackcacherelease implement a global cache of stack segments. // The cache is required to prevent unlimited growth of per-thread caches. static void stackcacherefill(void) { StackCacheNode *n; int32 i, pos; runtime·lock(&stackcachemu); n = stackcache; if(n) stackcache = n->next; runtime·unlock(&stackcachemu); if(n == nil) { n = (StackCacheNode*)runtime·SysAlloc(FixedStack*StackCacheBatch); if(n == nil) runtime·throw("out of memory (stackcacherefill)"); runtime·xadd64(&mstats.stacks_sys, FixedStack*StackCacheBatch); for(i = 0; i < StackCacheBatch-1; i++) n->batch[i] = (byte*)n + (i+1)*FixedStack; } pos = m->stackcachepos; for(i = 0; i < StackCacheBatch-1; i++) { m->stackcache[pos] = n->batch[i]; pos = (pos + 1) % StackCacheSize; } m->stackcache[pos] = n; pos = (pos + 1) % StackCacheSize; m->stackcachepos = pos; m->stackcachecnt += StackCacheBatch; }
runtime·MHeap_SysAlloc ( MHeap *h , uintptr n ) { byte *p; #line 353 "malloc.goc" if ( n <= h->arena_end - h->arena_used ) { #line 355 "malloc.goc" p = h->arena_used; runtime·SysMap ( p , n ) ; h->arena_used += n; runtime·MHeap_MapBits ( h ) ; return p; } #line 363 "malloc.goc" if ( sizeof ( void* ) == 8 ) return nil; #line 369 "malloc.goc" p = runtime·SysAlloc ( n ) ; if ( p == nil ) return nil; #line 373 "malloc.goc" if ( p < h->arena_start || p+n - h->arena_start >= MaxArena32 ) { runtime·printf ( "runtime: memory allocated by OS not in usable range\n" ) ; runtime·SysFree ( p , n ) ; return nil; } #line 379 "malloc.goc" if ( p+n > h->arena_used ) { h->arena_used = p+n; if ( h->arena_used > h->arena_end ) h->arena_end = h->arena_used; runtime·MHeap_MapBits ( h ) ; } #line 386 "malloc.goc" return p; }
static int HashGrow(Hash *pHash) { unsigned long i, ulHIdx, ulHMask; SysListHead *pBkts, *pHead, *pPos; HashNode *pHNode; ulHMask = pHash->ulHMask + 1; if (ulHMask & HMASK_TOP_BIT) return 0; ulHMask = (ulHMask << 1) - 1; if ((pBkts = (SysListHead *) SysAlloc((ulHMask + 1) * sizeof(SysListHead))) == NULL) return ErrGetErrorCode(); for (i = 0; i <= ulHMask; i++) SYS_INIT_LIST_HEAD(&pBkts[i]); for (i = 0; i <= pHash->ulHMask; i++) { pHead = &pHash->pBkts[i]; while ((pPos = SYS_LIST_FIRST(pHead)) != NULL) { pHNode = SYS_LIST_ENTRY(pPos, HashNode, Lnk); SYS_LIST_DEL(&pHNode->Lnk); ulHIdx = (*pHash->Ops.pGetHashVal)(pHash->Ops.pPrivate, &pHNode->Key) & ulHMask; SYS_LIST_ADDT(&pHNode->Lnk, &pBkts[ulHIdx]); } } SysFree(pHash->pBkts); pHash->pBkts = pBkts; pHash->ulHMask = ulHMask; return 0; }
static void RecordSpan(void *vh, byte *p) { MHeap *h; MSpan *s; MSpan **all; uint32 cap; h = vh; s = (MSpan*)p; if(h->nspan >= h->nspancap) { cap = 64*1024/sizeof(all[0]); if(cap < h->nspancap*3/2) cap = h->nspancap*3/2; all = (MSpan**)runtime·SysAlloc(cap*sizeof(all[0]), &mstats.other_sys); if(all == nil) runtime·throw("runtime: cannot allocate memory"); if(h->allspans) { runtime·memmove(all, h->allspans, h->nspancap*sizeof(all[0])); // Don't free the old array if it's referenced by sweep. // See the comment in mgc0.c. if(h->allspans != runtime·mheap.sweepspans) runtime·SysFree(h->allspans, h->nspancap*sizeof(all[0]), &mstats.other_sys); } h->allspans = all; h->nspancap = cap; } h->allspans[h->nspan++] = s; }
static void RecordSpan(void *vh, byte *p) { MHeap *h; MSpan *s; MSpan **all; uint32 cap; h = vh; s = (MSpan*)p; if(h->nspan >= h->nspancap) { cap = 64*1024/sizeof(all[0]); if(cap < h->nspancap*3/2) cap = h->nspancap*3/2; all = (MSpan**)runtime·SysAlloc(cap*sizeof(all[0])); if(all == nil) runtime·throw("runtime: cannot allocate memory"); if(h->allspans) { runtime·memmove(all, h->allspans, h->nspancap*sizeof(all[0])); runtime·SysFree(h->allspans, h->nspancap*sizeof(all[0])); } h->allspans = all; h->nspancap = cap; } h->allspans[h->nspan++] = s; }
int BSckBufferInit(BSockLineBuffer *pBLB, int iSize) { if (iSize <= 0) iSize = BSOCK_STD_BUFFER_SIZE; if ((pBLB->pszBuffer = (char *) SysAlloc(iSize)) == NULL) return ErrGetErrorCode(); pBLB->iSize = iSize; return 0; }
HASH_HANDLE HashCreate(HashOps const *pOps, unsigned long ulSize) { unsigned long i, ulHMask; Hash *pHash; if ((pHash = (Hash *) SysAlloc(sizeof(Hash))) == NULL) return INVALID_HASH_HANDLE; pHash->Ops = *pOps; for (ulHMask = 2; !(ulHMask & HMASK_TOP_BIT) && ulHMask <= ulSize; ulHMask <<= 1); ulHMask--; pHash->ulHMask = ulHMask; if ((pHash->pBkts = (SysListHead *) SysAlloc((ulHMask + 1) * sizeof(SysListHead))) == NULL) { SysFree(pHash); return INVALID_HASH_HANDLE; } for (i = 0; i <= ulHMask; i++) SYS_INIT_LIST_HEAD(&pHash->pBkts[i]); return (HASH_HANDLE) pHash; }
runtime·stackalloc(G *gp, uint32 n) { uint32 pos; void *v; bool malloced; Stktop *top; // Stackalloc must be called on scheduler stack, so that we // never try to grow the stack during the code that stackalloc runs. // Doing so would cause a deadlock (issue 1547). if(g != m->g0) runtime·throw("stackalloc not on scheduler stack"); if((n & (n-1)) != 0) runtime·throw("stack size not a power of 2"); if(StackDebug >= 1) runtime·printf("stackalloc %d\n", n); gp->stacksize += n; if(runtime·debug.efence || StackFromSystem) { v = runtime·SysAlloc(ROUND(n, PageSize), &mstats.stacks_sys); if(v == nil) runtime·throw("out of memory (stackalloc)"); return v; } // Minimum-sized stacks are allocated with a fixed-size free-list allocator, // but if we need a stack of a bigger size, we fall back on malloc // (assuming that inside malloc all the stack frames are small, // so that we do not deadlock). malloced = true; if(n == FixedStack || m->mallocing) { if(n != FixedStack) { runtime·printf("stackalloc: in malloc, size=%d want %d\n", FixedStack, n); runtime·throw("stackalloc"); } if(m->stackcachecnt == 0) stackcacherefill(); pos = m->stackcachepos; pos = (pos - 1) % StackCacheSize; v = m->stackcache[pos]; m->stackcachepos = pos; m->stackcachecnt--; m->stackinuse++; malloced = false; } else v = runtime·mallocgc(n, 0, FlagNoProfiling|FlagNoGC|FlagNoZero|FlagNoInvokeGC); top = (Stktop*)((byte*)v+n-sizeof(Stktop)); runtime·memclr((byte*)top, sizeof(*top)); top->malloced = malloced; return v; }
runtime·MHeap_SysAlloc ( MHeap *h , uintptr n ) { byte *p; #line 2418 "C:\Go\src\pkg\runtime\malloc.goc" if ( n > h->arena_end - h->arena_used ) { #line 2421 "C:\Go\src\pkg\runtime\malloc.goc" byte *new_end; uintptr needed; #line 2424 "C:\Go\src\pkg\runtime\malloc.goc" needed = ( uintptr ) h->arena_used + n - ( uintptr ) h->arena_end; #line 2426 "C:\Go\src\pkg\runtime\malloc.goc" needed = ( needed + ( 256<<20 ) - 1 ) & ~ ( ( 256<<20 ) -1 ) ; new_end = h->arena_end + needed; if ( new_end <= h->arena_start + MaxArena32 ) { p = runtime·SysReserve ( h->arena_end , new_end - h->arena_end ) ; if ( p == h->arena_end ) h->arena_end = new_end; } } if ( n <= h->arena_end - h->arena_used ) { #line 2436 "C:\Go\src\pkg\runtime\malloc.goc" p = h->arena_used; runtime·SysMap ( p , n ) ; h->arena_used += n; runtime·MHeap_MapBits ( h ) ; return p; } #line 2444 "C:\Go\src\pkg\runtime\malloc.goc" if ( sizeof ( void* ) == 8 && ( uintptr ) h->bitmap >= 0xffffffffU ) return nil; #line 2450 "C:\Go\src\pkg\runtime\malloc.goc" p = runtime·SysAlloc ( n ) ; if ( p == nil ) return nil; #line 2454 "C:\Go\src\pkg\runtime\malloc.goc" if ( p < h->arena_start || p+n - h->arena_start >= MaxArena32 ) { runtime·printf ( "runtime: memory allocated by OS (%p) not in usable range [%p,%p)\n" , p , h->arena_start , h->arena_start+MaxArena32 ) ; runtime·SysFree ( p , n ) ; return nil; } #line 2461 "C:\Go\src\pkg\runtime\malloc.goc" if ( p+n > h->arena_used ) { h->arena_used = p+n; if ( h->arena_used > h->arena_end ) h->arena_end = h->arena_used; runtime·MHeap_MapBits ( h ) ; } #line 2468 "C:\Go\src\pkg\runtime\malloc.goc" return p; }
static int UPopFillMessageList(char const *pszBasePath, char const *pszSubPath, SysListHead *pMsgList, int &iMsgCount, SYS_OFF_T &llMBSize) { SYS_HANDLE hFind; char szScanPath[SYS_MAX_PATH], szFileName[SYS_MAX_PATH]; if (pszSubPath == NULL) StrSNCpy(szScanPath, pszBasePath); else SysSNPrintf(szScanPath, sizeof(szScanPath) - 1, "%s" SYS_SLASH_STR "%s", pszBasePath, pszSubPath); if ((hFind = SysFirstFile(szScanPath, szFileName, sizeof(szFileName))) != SYS_INVALID_HANDLE) { do { POP3MsgData *pPOPMD; if (SysIsDirectory(hFind) || !UPopMailFileNameFilter(szFileName)) continue; if ((pPOPMD = (POP3MsgData *) SysAlloc(sizeof(POP3MsgData))) == NULL) { ErrorPush(); SysFindClose(hFind); return ErrorPop(); } if (pszSubPath == NULL) StrSNCpy(pPOPMD->szMsgName, szFileName); else SysSNPrintf(pPOPMD->szMsgName, sizeof(pPOPMD->szMsgName) - 1, "%s" SYS_SLASH_STR "%s", pszSubPath, szFileName); pPOPMD->llMsgSize = SysGetSize(hFind); pPOPMD->ulFlags = 0; /* Insert entry in message list */ SYS_LIST_ADDT(&pPOPMD->LLnk, pMsgList); /* Update mailbox information */ llMBSize += pPOPMD->llMsgSize; ++iMsgCount; } while (SysNextFile(hFind, szFileName, sizeof(szFileName))); SysFindClose(hFind); } return 0; }
PLISTLINK *ListGetPointers(HSLIST &hList, int &iListCount) { iListCount = ListGetCount(hList); PLISTLINK *pPointers = (PLISTLINK *) SysAlloc((iListCount + 1) * sizeof(PLISTLINK)); if (pPointers != NULL) { int i; PLISTLINK lpCurr = ListFirst(hList); for (i = 0; lpCurr != INVALID_SLIST_PTR; lpCurr = ListNext(hList, lpCurr), i++) pPointers[i] = lpCurr; pPointers[i] = INVALID_SLIST_PTR; } return pPointers; }
static int BSslAllocCtx(SslBindCtx **ppCtx, SYS_SOCKET SockFD, SSL_CTX *pSCtx, SSL *pSSL) { SslBindCtx *pCtx; if ((pCtx = (SslBindCtx *) SysAlloc(sizeof(SslBindCtx))) == NULL) return ErrGetErrorCode(); pCtx->IOOps.pPrivate = pCtx; pCtx->IOOps.pName = BSslCtx__Name; pCtx->IOOps.pFree = BSslCtx__Free; pCtx->IOOps.pRead = BSslCtx__Read; pCtx->IOOps.pWrite = BSslCtx__Write; pCtx->IOOps.pSendFile = BSslCtx__SendFile; pCtx->SockFD = SockFD; pCtx->pSCtx = pSCtx; pCtx->pSSL = pSSL; *ppCtx = pCtx; return 0; }
int RLckInitLockers(void) { /* Create resource locking mutex */ if ((hRLMutex = SysCreateMutex()) == SYS_INVALID_MUTEX) return ErrGetErrorCode(); /* Initialize wait gates */ for (int i = 0; i < STD_WAIT_GATES; i++) { if ((RLGates[i].hSemaphore = SysCreateSemaphore(0, SYS_DEFAULT_MAXCOUNT)) == SYS_INVALID_SEMAPHORE) { ErrorPush(); for (--i; i >= 0; i--) { SysFree(RLGates[i].pResList); SysCloseSemaphore(RLGates[i].hSemaphore); } SysCloseMutex(hRLMutex); return ErrorPop(); } RLGates[i].iWaitingProcesses = 0; RLGates[i].iHashSize = STD_RES_HASH_SIZE; if ((RLGates[i].pResList = (SysListHead *) SysAlloc(RLGates[i].iHashSize * sizeof(SysListHead))) == NULL) { ErrorPush(); SysCloseSemaphore(RLGates[i].hSemaphore); for (--i; i >= 0; i--) { SysFree(RLGates[i].pResList); SysCloseSemaphore(RLGates[i].hSemaphore); } SysCloseMutex(hRLMutex); return ErrorPop(); } for (int j = 0; j < RLGates[i].iHashSize; j++) SYS_INIT_LIST_HEAD(&RLGates[i].pResList[j]); } return 0; }
int BSckSendString(BSOCK_HANDLE hBSock, char const *pszBuffer, int iTimeout) { BuffSocketData *pBSD = (BuffSocketData *) hBSock; char *pszSendBuffer = (char *) SysAlloc(strlen(pszBuffer) + 3); if (pszSendBuffer == NULL) return ErrGetErrorCode(); SysLogMessage(LOG_LEV_DEBUG, "socket write line: [%s]\n", pszBuffer); sprintf(pszSendBuffer, "%s\r\n", pszBuffer); int iSendLength = (int)strlen(pszSendBuffer); if (BSckWriteLL(pBSD, pszSendBuffer, iSendLength, iTimeout) != iSendLength) { SysFree(pszSendBuffer); return ErrGetErrorCode(); } SysFree(pszSendBuffer); return iSendLength; }
static PollDesc* allocPollDesc ( void ) { PollDesc *pd; uint32 i , n; #line 334 "/tmp/bindist907131767/go/src/pkg/runtime/netpoll.goc" runtime·lock ( &pollcache ) ; if ( pollcache.first == nil ) { n = PageSize/sizeof ( *pd ) ; if ( n == 0 ) n = 1; #line 341 "/tmp/bindist907131767/go/src/pkg/runtime/netpoll.goc" pd = runtime·SysAlloc ( n*sizeof ( *pd ) ) ; for ( i = 0; i < n; i++ ) { pd[i].link = pollcache.first; pollcache.first = &pd[i]; } } pd = pollcache.first; pollcache.first = pd->link; runtime·unlock ( &pollcache ) ; return pd; }
int BSslInit(void) { int i, iNumLocks = CRYPTO_num_locks(); if ((pSslMtxs = (SYS_MUTEX *) SysAlloc(iNumLocks * sizeof(SYS_MUTEX))) == NULL) return ErrGetErrorCode(); for (i = 0; i < iNumLocks; i++) { if ((pSslMtxs[i] = SysCreateMutex()) == SYS_INVALID_MUTEX) { ErrorPush(); for (i--; i >= 0; i--) SysCloseMutex(pSslMtxs[i]); SysFreeNullify(pSslMtxs); return ErrorPop(); } } SSL_load_error_strings(); SSLeay_add_ssl_algorithms(); CRYPTO_set_id_callback(SysGetCurrentThreadId); CRYPTO_set_locking_callback(BSslLockingCB); SysAddThreadExitHook(BSslThreadExit, NULL); return 0; }