void* wrap_alloc(T&& func, void* ptr, size_t bytes) { size_t mb = get_max_bytes(); size_t tb = get_total_bytes(); if (mb && (tb + bytes > mb)) { auto scope = mongo::mozjs::MozJSImplScope::getThreadScope(); if (scope) scope->setOOM(); // We fall through here because we want to let spidermonkey continue // with whatever it was doing. Calling setOOM will fail the top level // operation as soon as possible. } #ifdef MONGO_NO_MALLOC_USABLE_SIZE void* p = func(ptr ? static_cast<char*>(ptr) - kMaxAlign : nullptr, bytes + kMaxAlign); #else void* p = func(ptr, bytes); #endif if (!p) { return nullptr; } #ifdef MONGO_NO_MALLOC_USABLE_SIZE *reinterpret_cast<size_t*>(p) = bytes; p = static_cast<char*>(p) + kMaxAlign; #endif total_bytes = tb + bytes; return p; }
void print_fields(char *line) { char *line_char = NULL; char *tmp = NULL; int st_idx = 0; int total_bytes = 0; printf("%s ", line); total_bytes = get_total_bytes(line); tmp = malloc(sizeof(char) * total_bytes + 1); strncpy(tmp, line, total_bytes); tmp[total_bytes + 1] = '\0'; tbl_info.start_state = atoi(tmp); free(tmp); st_idx += total_bytes + 1; total_bytes = get_total_bytes(&line[st_idx]); strncpy(&tbl_info.read, &line[st_idx], total_bytes); st_idx += total_bytes + 1; total_bytes = get_total_bytes(&line[st_idx]); strncpy(&tbl_info.write, &line[st_idx], total_bytes); st_idx += total_bytes + 1; total_bytes = get_total_bytes(&line[st_idx]); tmp = malloc(sizeof(char) * total_bytes + 1); tmp[total_bytes + 1] = '\0'; tbl_info.direction = atoi(tmp); free(tmp); st_idx += total_bytes + 1; total_bytes = get_total_bytes(&line[st_idx]); tmp = malloc(sizeof(char) * total_bytes + 1); tmp[total_bytes + 1] = '\0'; tbl_info.next_state = atoi(tmp); free(tmp); printf("new state: %d", tbl_info.start_state); printf("Read: %c", tbl_info.read); }
void SrsKbpsSlice::sample() { int64_t now = srs_get_system_time_ms(); int64_t total_bytes = get_total_bytes(); if (sample_30s.time <= 0) { sample_30s.kbps = 0; sample_30s.time = now; sample_30s.bytes = total_bytes; } if (sample_1m.time <= 0) { sample_1m.kbps = 0; sample_1m.time = now; sample_1m.bytes = total_bytes; } if (sample_5m.time <= 0) { sample_5m.kbps = 0; sample_5m.time = now; sample_5m.bytes = total_bytes; } if (sample_60m.time <= 0) { sample_60m.kbps = 0; sample_60m.time = now; sample_60m.bytes = total_bytes; } if (now - sample_30s.time > 30 * 1000) { sample_30s.kbps = (int)((total_bytes - sample_30s.bytes) * 8 / (now - sample_30s.time)); sample_30s.time = now; sample_30s.bytes = total_bytes; } if (now - sample_1m.time > 60 * 1000) { sample_1m.kbps = (int)((total_bytes - sample_1m.bytes) * 8 / (now - sample_1m.time)); sample_1m.time = now; sample_1m.bytes = total_bytes; } if (now - sample_5m.time > 300 * 1000) { sample_5m.kbps = (int)((total_bytes - sample_5m.bytes) * 8 / (now - sample_5m.time)); sample_5m.time = now; sample_5m.bytes = total_bytes; } if (now - sample_60m.time > 3600 * 1000) { sample_60m.kbps = (int)((total_bytes - sample_60m.bytes) * 8 / (now - sample_60m.time)); sample_60m.time = now; sample_60m.bytes = total_bytes; } }