void *BT_kRealloc(void *p, BT_u32 ulSize) { void *n = NULL; // CRITICAL SECTION BT_kEnterCritical(); { if((p) && (ulSize)) { n = BT_kMalloc(ulSize); if (n) { BT_HEAP_BLOCK * pOld = (BT_HEAP_BLOCK *)p; pOld--; pOld->ulSize -= sizeof(BT_HEAP_BLOCK); if (ulSize > pOld->ulSize) memcpy(n, p, pOld->ulSize); else memcpy(n, p, ulSize); } } else if((p) && (!ulSize)) { BT_kFree(p); } else if((!p) && (ulSize)) { n = BT_kMalloc(ulSize); } } BT_kExitCritical(); return n; }
BT_ERROR bt_of_i2c_populate_device(struct bt_device_node *device) { BT_RESOURCE *res; BT_u32 len; if(device->ulFlags & BT_DEVICE_POPULATED) { return BT_ERR_NONE; } const BT_be32 *paddr = bt_of_get_property(device, "reg", &len); if(len != 4) { return BT_ERR_INVALID_VALUE; } device->dev.ulTotalResources = 1; device->dev.pResources = BT_kMalloc(sizeof(BT_RESOURCE) * device->dev.ulTotalResources); device->dev.name = bt_of_get_property(device, "compatible", NULL); res = (BT_RESOURCE *) device->dev.pResources; res->ulStart = bt_be32_to_cpu(*paddr); res->ulEnd = device->dev.pResources->ulStart; res->ulFlags = BT_RESOURCE_ADDR; device->dev.eType = BT_DEVICE_I2C | BT_DEVICE_TYPE_OF_FLAG; device->ulFlags |= BT_DEVICE_POPULATED; return BT_ERR_NONE; }
static BT_HANDLE fullfat_mount(BT_HANDLE hFS, BT_HANDLE hVolume, BT_ERROR *pError) { FF_ERROR ffError; BT_ERROR Error = BT_ERR_GENERIC; BT_FF_MOUNT *pMount = (BT_FF_MOUNT *) BT_CreateHandle(&oHandleInterface, sizeof(BT_FF_MOUNT), pError); if(!pMount) { return NULL; } pMount->pBlockCache = BT_kMalloc(8192); pMount->hVolume = hVolume; pMount->pIoman = FF_CreateIOMAN(pMount->pBlockCache, 8192, 512, &ffError); if(!pMount->pIoman) { Error = BT_ERR_GENERIC; goto err_free_out; } ffError = FF_RegisterBlkDevice(pMount->pIoman, 512, fullfat_writeblocks, fullfat_readblocks, pMount); ffError = FF_MountPartition(pMount->pIoman, 0); return (BT_HANDLE) pMount; err_free_out: BT_kFree(pMount->pBlockCache); BT_DestroyHandle((BT_HANDLE)pMount); *pError = Error; return NULL; }
static int bt_setenv(int argc, char **argv) { BT_u32 total_length = 0; BT_u32 i; for(i = 2; i < argc; i++) { total_length += strlen(argv[i]) + 1; // Add a space after each item. } if(!total_length) { clear_env(argv[1]); return 0; } char *s = BT_kMalloc(total_length+1); if(!s) { bt_printf("No memory could not allocated temporary memory to construct string\n"); return -1; } strcpy(s, ""); for(i = 2; i < argc; i++) { strcat(s, argv[i]); if(i == argc-1) { break; } strcat(s, " "); } BT_ShellSetEnv(argv[1], s, BT_ENV_T_STRING); BT_kFree(s); return 0; }
static BT_HANDLE ext2_opendir(BT_HANDLE hMount, const BT_i8 *szpPath, BT_ERROR *pError) { if(!hMount) { if(pError) *pError = BT_ERR_GENERIC; goto err_out; } BT_EXT2_DIR *pDir = (BT_EXT2_DIR *) BT_CreateHandle(&oDirHandleInterface, sizeof(BT_EXT2_DIR), pError); if(!pDir) { *pError = BT_ERR_NO_MEMORY; goto err_out; } pDir->pMount = (BT_EXT2_MOUNT *)hMount; pDir->szpFname = BT_kMalloc(BT_EXT2_FNAME_MAX_STRLEN); if(!pDir->szpFname) { *pError = BT_ERR_NO_MEMORY; goto err_free_out; } if(ext2fs_open_dir((char *)szpPath) < 0) { if(pError) *pError = BT_ERR_GENERIC; goto err_free_out; } #if 0 if(ext2fs_ls((char *)szpPath) < 0) { if(pError) *pError = BT_ERR_GENERIC; goto err_free_out; } #endif return (BT_HANDLE) pDir; err_free_out: if(pDir->szpFname) BT_kFree(pDir->szpFname); BT_DestroyHandle((BT_HANDLE)pDir); err_out: return NULL; }
void *BT_Calloc(BT_u32 ulSize) { void *p = BT_kMalloc(ulSize); if(p) { memset(p, 0, ulSize); } return p; }
void *BT_kRealloc(void *p, BT_u32 ulSize) { void *n = NULL; if((p) && (ulSize)) { n = BT_kMalloc(ulSize); if (n) { struct MEM_TAG *tag = (struct MEM_TAG *) p; tag -= 1; if (ulSize > tag->size) memcpy(n, p, tag->size); else memcpy(n, p, ulSize); } } else if((p) && (!ulSize)) { BT_kFree(p); } else if((!p) && (ulSize)) { n = BT_kMalloc(ulSize); } return n; }
static struct bt_segment *bt_segment_create(struct bt_segment *prev, bt_vaddr_t addr, BT_u32 size) { struct bt_segment *seg; seg = BT_kMalloc(sizeof(struct bt_segment)); if(!seg) { return NULL; } seg->addr = addr; seg->size = size; seg->phys = 0; seg->flags = BT_SEG_FREE; bt_list_add(&seg->list, &prev->list); return seg; }
static int bt_partition_disk(BT_HANDLE hShell, int argc, char **argv) { if(argc > 2) { usage_disk(hShell); return -1; } if(argc == 1) { BT_PRSHELL("Current disk: %s\n", device_path ? device_path : "none-selected"); return 0; } if(!device_path) { int retval = 0; BT_ERROR Error; BT_HANDLE hBlock = BT_Open(argv[1], 0, &Error); if(!hBlock) { BT_PRSHELL("Error: Could not open %s\n", argv[1]); return -1; } /** * Ensure we have a valid block device handle. * This API will return BT_ERR_INVALID_HANDLE, or BT_ERR_NONE. * * Maybe we should actually add a simple API for that! **/ if(BT_GetBlockGeometry(hBlock, NULL) != BT_ERR_NONE) { BT_PRSHELL("Error: %s is not a RAW block device\n", argv[1]); retval = -1; goto close_out; } device_path = BT_kMalloc(strlen(argv[1])); strcpy(device_path, argv[1]); close_out: BT_CloseHandle(hBlock); return retval; } else { BT_PRSHELL("Already working with: %s\n", device_path); } return 0; }
BT_ERROR BT_ShellScript(const BT_i8 *path) { BT_ERROR Error; BT_HANDLE hFile = BT_Open(path, "rb", &Error); if(!hFile) { BT_kPrint("Could not open shell script %s\n", path); return BT_ERR_GENERIC; } BT_i8 *line = BT_kMalloc(256); if(!line) { BT_CloseHandle(hFile); return BT_ERR_NO_MEMORY; } BT_u32 linelen; while((linelen = BT_GetS(hFile, 256, line)) > 0) { BT_i8 *p = line; while(isspace((int) *p)) { p++; } if(*p == '#') { continue; // commented line! } if(p == (line + linelen)) { continue; } Error = BT_ShellCommand(p); } BT_kFree(line); BT_CloseHandle(hFile); return BT_ERR_NONE; }
static int bt_ps(BT_HANDLE hShell, int argc, char **argv) { BT_HANDLE hStdout = BT_ShellGetStdout(hShell); BT_u32 i = 0; struct bt_process_time oTime; BT_u32 total_processes = BT_GetTotalProcesses(); struct process_time *runtimes = BT_kMalloc(sizeof(struct process_time) * total_processes); if(!runtimes) { return -1; } BT_u64 runtime = BT_GetGlobalTimer(); for(i = 0; i < total_processes; i++) { BT_GetProcessTime(&oTime, i); runtimes[i].ullRuntimeCounter = oTime.ullRunTimeCounter; } BT_ThreadSleep(1000); runtime = BT_GetGlobalTimer() - runtime; for(i = 0; i < total_processes; i++) { BT_GetProcessTime(&oTime, i); runtimes[i].ullRuntimeCounter = oTime.ullRunTimeCounter - runtimes[i].ullRuntimeCounter; } for(i = 0; i < total_processes; i++) { BT_GetProcessTime(&oTime, i); bt_fprintf(hStdout, "%s : %d%%\n", oTime.name, runtimes[i].ullRuntimeCounter / (runtime / 100)); } bt_fprintf(hStdout, "Total runtime %d seconds\n", (BT_u32) (BT_GetGlobalTimer() / (BT_u64)BT_GetGlobalTimerRate())); BT_kFree(runtimes); return 0; }
static int fdt_parse_property(BT_HANDLE hStdout, char **values, int count, char **data, int *len, int *bAllocated) { char *valp = values[0]; int stridx = 0; *len = 0; *bAllocated = 0; if(values[0][0] == '<') { // Array of cells dec/hex // Assume data required == (count - 2) * 4 bytes + 8 for good measure. *data = BT_kMalloc((sizeof(BT_u32) * (count - 2)) + 8); *bAllocated = 1; char *dat = *data; valp++; while((stridx < count) && (*valp != '>')) { if(!*valp) { valp = values[++stridx]; continue; } char *copy = valp; BT_u32 val = strtoul(valp, &valp, 0); *(BT_be32 *) dat = bt_cpu_to_be32(val); dat += 4; *len += 4; if((valp - copy) <= 0) { bt_fprintf(hStdout, "Could not convert \"%s\"\n", copy); return -1; } } if(*valp != '>') { bt_fprintf(hStdout, "Unexpected character %c\n", *valp); return -1; } } else if(values[0][0] == '[') { // Byte stream, (just hex) // Assume data required == (count - 2) * 1bytes, + 4 bytes for good measure *data = BT_kMalloc((sizeof(char) * (count - 2)) + 4); *bAllocated = 1; char *dat = *data; valp++; while((stridx < count) && (*valp != ']')) { if(!*valp) { valp = values[++stridx]; continue; } if(!isxdigit((int)*valp)) { break; } int temp = strtoul(valp, &valp, 16); *dat++ = (char) (temp & 0xFF); *len = *len + 1; } if(*valp != ']') { bt_fprintf(hStdout, "Unexpected character '%c'\n", *valp); return -1; } } else { // Assume a string to be copied directly to data! *data = values[0]; *bAllocated = 0; *len += strlen(values[0]) + 1; } return 0; }
BT_ERROR BT_ShellCommand(char *input) { BT_u32 ulArguments = 0; BT_u32 bIsArg = BT_FALSE; char *copy = BT_kMalloc(strlen(input)+1); if(!copy) { return BT_ERR_NO_MEMORY; } strcpy(copy, input); input = copy; while(isspace((int)*input)) { input++; // Eat up prefixed whitespace. } bIsArg = BT_TRUE; char *line = input; while(*input) { if(bIsArg) { if(isspace((int)*input)) { ulArguments += 1; bIsArg = BT_FALSE; } } else { if(!isspace((int)*input)) { bIsArg = BT_TRUE; } } input++; } if(bIsArg) { ulArguments += 1; } char **pargs = BT_kMalloc(sizeof(char *) * ulArguments); if(!pargs) { return BT_ERR_NO_MEMORY; } input = line; while(!*input); bIsArg = BT_FALSE; BT_u32 i = 0; while(*input) { if(!bIsArg) { if(!isspace((int)*input)) { bIsArg = BT_TRUE; pargs[i++] = input; } } else { if(isspace((int)*input)) { *input = '\0'; bIsArg = BT_FALSE; } } input++; } const BT_SHELL_COMMAND *pCommand = GetShellCommand(pargs[0]); if(!pCommand) { BT_kFree(pargs); BT_kFree(copy); return BT_ERR_NONE; } pCommand->pfnCommand(ulArguments, pargs); BT_kFree(pargs); BT_kFree(copy); return BT_ERR_NONE; }
void *pvPortMalloc(int size) { return BT_kMalloc(size); }
/** * Force Malloc and free to be forced onto our own APIs. * **/ void *malloc(int size) { return BT_kMalloc(size); }