void* lmemory_malloc(size_t size) { #ifdef ARK_ALLOC_DETAIL_LOG char logbuf[128]; void* ptr = ff_malloc(&bucket, size); sprintf(logbuf, "malloc(%08X) => %08X", size, ptr); ff_alloc_detail_log(logbuf); return ptr; #else return ff_malloc(&bucket, size); #endif }
struct ff_file *ff_file_open(const wchar_t *path, enum ff_file_access_mode access_mode) { struct ff_file *file; struct ff_arch_file *arch_file; enum ff_arch_file_access_mode arch_access_mode; file = NULL; arch_access_mode = access_mode == FF_FILE_READ ? FF_ARCH_FILE_READ : FF_ARCH_FILE_WRITE; arch_file = ff_arch_file_open(path, arch_access_mode); if (arch_file == NULL) { const char *mode; mode = (access_mode == FF_FILE_READ) ? "reading" : "writing"; ff_log_debug(L"cannot open the file=[%ls] for %hs. See previous messages for more info", path, mode); goto end; } file = (struct ff_file *) ff_malloc(sizeof(*file)); file->file = arch_file; file->access_mode = access_mode; if (access_mode == FF_FILE_READ) { file->buffers.read_buffer = ff_read_stream_buffer_create(file_read_func, file, BUFFER_SIZE); } else { file->buffers.write_buffer = ff_write_stream_buffer_create(file_write_func, file, BUFFER_SIZE); } end: return file; }
struct ff_arch_net_addr *ff_arch_net_addr_create() { struct ff_arch_net_addr *addr; addr = (struct ff_arch_net_addr *) ff_malloc(sizeof(*addr)); return addr; }
struct ff_queue *ff_queue_create() { struct ff_queue *queue; queue = (struct ff_queue *) ff_malloc(sizeof(*queue)); queue->front = NULL; queue->back_ptr = &queue->front; return queue; }
struct ff_stream_connector *ff_stream_connector_create(const struct ff_stream_connector_vtable *vtable, void *ctx) { struct ff_stream_connector *stream_connector; stream_connector = (struct ff_stream_connector *) ff_malloc(sizeof(*stream_connector)); stream_connector->vtable = vtable; stream_connector->ctx = ctx; return stream_connector; }
struct ff_arch_fiber *ff_arch_fiber_create(ff_arch_fiber_func arch_fiber_func, void *ctx, int stack_size) { struct ff_arch_fiber *fiber; ff_assert(stack_size > 0); fiber = (struct ff_arch_fiber *) ff_malloc(sizeof(*fiber)); fiber->handle = CreateFiber(stack_size, (LPFIBER_START_ROUTINE) arch_fiber_func, ctx); ff_winapi_fatal_error_check(fiber->handle != NULL, L"cannot create new fiber"); return fiber; }
void ff_queue_push(struct ff_queue *queue, const void *data) { struct queue_entry *entry; entry = (struct queue_entry *) ff_malloc(sizeof(*entry)); entry->next = NULL; entry->data = data; *queue->back_ptr = entry; queue->back_ptr = &entry->next; }
struct ff_arch_thread *ff_arch_thread_create(ff_arch_thread_func func, int stack_size) { struct ff_arch_thread *thread; thread = (struct ff_arch_thread *) ff_malloc(sizeof(*thread)); thread->handle = (HANDLE) _beginthreadex(NULL, stack_size, generic_thread_func, thread, CREATE_SUSPENDED, NULL); ff_winapi_fatal_error_check(thread->handle != NULL, L"cannot start new thread"); thread->func = func; thread->ctx = NULL; return thread; }
struct ff_event *ff_event_create(enum ff_event_type event_type) { struct ff_event *event; event = (struct ff_event *) ff_malloc(sizeof(*event)); event->pending_fibers = ff_stack_create(); event->event_type = event_type; event->is_set = 0; return event; }
struct mrpc_client *mrpc_client_create() { struct mrpc_client *client; client = (struct mrpc_client *) ff_malloc(sizeof(*client)); client->stop_event = ff_event_create(FF_EVENT_AUTO); client->stream_processor = mrpc_client_stream_processor_create(); client->stream_connector = NULL; return client; }
static struct mrpc_blob *create_blob(int len) { struct mrpc_blob *blob; ff_assert(len >= 0); blob = (struct mrpc_blob *) ff_malloc(sizeof(*blob)); blob->file_path = create_temporary_file_path(); blob->len = len; blob->state = BLOB_EMPTY; blob->ref_cnt = 1; return blob; }
struct ff_blocking_stack *ff_blocking_stack_create(int max_size) { struct ff_blocking_stack *stack; ff_assert(max_size > 0); stack = (struct ff_blocking_stack *) ff_malloc(sizeof(*stack)); stack->simple_stack = ff_stack_create(); stack->producer_semaphore = ff_semaphore_create(0); stack->consumer_semaphore = ff_semaphore_create(max_size); return stack; }
struct mrpc_bitmap *mrpc_bitmap_create(int size) { struct mrpc_bitmap *bitmap; int map_size; ff_assert(size > 0); map_size = GET_U64_ARRAY_SIZE(size); bitmap = (struct mrpc_bitmap *) ff_malloc(sizeof(*bitmap)); bitmap->map = (uint64_t *) ff_calloc(map_size, sizeof(bitmap->map[0])); bitmap->size = size; bitmap->last_free_bit = 0; return bitmap; }
struct ff_write_stream_buffer *ff_write_stream_buffer_create(ff_write_stream_func write_func, void *write_func_ctx, int capacity) { struct ff_write_stream_buffer *buffer; ff_assert(capacity > 0); buffer = (struct ff_write_stream_buffer *) ff_malloc(sizeof(*buffer)); buffer->write_func = write_func; buffer->write_func_ctx = write_func_ctx; buffer->buf = (char *) ff_calloc(capacity, sizeof(buffer->buf[0])); buffer->capacity = capacity; buffer->start_pos = 0; return buffer; }
struct ff_stream *mrpc_blob_open_stream(struct mrpc_blob *blob, enum mrpc_blob_open_stream_mode mode) { struct ff_stream *stream = NULL; struct blob_stream_data *data; struct ff_file *file; ff_assert(blob->ref_cnt > 0); if (mode == MRPC_BLOB_READ) { ff_assert(blob->state == BLOB_COMPLETE); mrpc_blob_inc_ref(blob); file = ff_file_open(blob->file_path, FF_FILE_READ); if (file == NULL) { ff_log_warning(L"cannot open the blob backing file [%ls] for reading", blob->file_path); mrpc_blob_dec_ref(blob); goto end; } } else { ff_assert(mode == MRPC_BLOB_WRITE); ff_assert(blob->ref_cnt == 1); ff_assert(blob->state == BLOB_EMPTY); mrpc_blob_inc_ref(blob); file = ff_file_open(blob->file_path, FF_FILE_WRITE); if (file == NULL) { ff_log_warning(L"cannot open the blob backing file [%ls] for writing", blob->file_path); mrpc_blob_dec_ref(blob); goto end; } blob->state = BLOB_INCOMPLETE; } data = (struct blob_stream_data *) ff_malloc(sizeof(*data)); data->blob = blob; data->file = file; data->mode = mode; data->curr_pos = 0; stream = ff_stream_create(&blob_stream_vtable, data); end: return stream; }
static void *create_request_stream(void *ctx) { struct mrpc_server_stream_processor *stream_processor; struct request_stream *request_stream; stream_processor = (struct mrpc_server_stream_processor *) ctx; ff_assert(stream_processor->state != STATE_STOPPED); /* The current stream processor can simultaneously use up to MAX_PACKETS_CNT packets. * So, it will be safe to set the reader queue's size to MAX_PACKETS_CNT. */ request_stream = (struct request_stream *) ff_malloc(sizeof(*request_stream)); request_stream->stream_processor = stream_processor; request_stream->packet_stream = mrpc_packet_stream_create(stream_processor->writer_queue, MAX_PACKETS_CNT, acquire_packet, release_packet, stream_processor); request_stream->stream = create_request_stream_wrapper(request_stream); request_stream->request_id = 0; return request_stream; }
static struct ff_arch_tcp *create_tcp(int sd) { struct ff_arch_tcp *tcp; int rv; int sd_rd, sd_wr; sd_rd = sd; rv = fcntl(sd_rd, F_SETFL, O_NONBLOCK); ff_linux_fatal_error_check(rv != -1, L"cannot set nonblocking mode for the TCP socket"); sd_wr = dup(sd_rd); ff_linux_fatal_error_check(sd_wr != -1, L"cannot duplicate TCP socket"); tcp = (struct ff_arch_tcp *) ff_malloc(sizeof(*tcp)); tcp->sd_rd = sd_rd; tcp->sd_wr = sd_wr; return tcp; }
struct mrpc_server_stream_processor *mrpc_server_stream_processor_create(mrpc_server_stream_processor_release_func release_func, void *release_func_ctx, mrpc_server_stream_processor_release_id_func release_id_func, void *release_id_func_ctx, int id) { struct mrpc_server_stream_processor *stream_processor; stream_processor = (struct mrpc_server_stream_processor *) ff_malloc(sizeof(*stream_processor)); stream_processor->release_func = release_func; stream_processor->release_func_ctx = release_func_ctx; stream_processor->release_id_func = release_id_func; stream_processor->release_id_func_ctx = release_id_func_ctx; stream_processor->writer_stop_event = ff_event_create(FF_EVENT_AUTO); stream_processor->request_streams_stop_event = ff_event_create(FF_EVENT_AUTO); stream_processor->request_streams_pool = ff_pool_create(MAX_REQUEST_STREAMS_CNT, create_request_stream, stream_processor, delete_request_stream); stream_processor->packets_pool = ff_pool_create(MAX_PACKETS_CNT, create_packet, stream_processor, delete_packet); /* in fact, writer_queue's size must be unlimited in order to avoid blocking of process_request_func on packet_stream flush, * which is the last operation before the client will receive response and will be able * to send new request with the same request_id to the server. If the worker * will block on the flush, then the server can receive new request with the request_id, * which belongs to request_stream, before the corresponding request stream will be released, * so it won't be able to process the new request, which will lead to client-server connection termination. * * However, the stream processor can operate only with packets from the packets_pool with * limited size (MAX_PACKETS_CNT). So, it is safe to set writer_queue's size to MAX_PACKETS_CNT, * because this limit won't never be overflowed. */ stream_processor->writer_queue = ff_blocking_queue_create(MAX_PACKETS_CNT); stream_processor->active_request_streams = (struct request_stream **) ff_calloc(MAX_REQUEST_STREAMS_CNT, sizeof(stream_processor->active_request_streams[0])); stream_processor->id = id; stream_processor->stream_handler = NULL; stream_processor->service_ctx = NULL; stream_processor->stream = NULL; stream_processor->active_request_streams_cnt = 0; stream_processor->state = STATE_STOPPED; return stream_processor; }