Пример #1
0
h_heap_st *h_heap_create(heap_comp_func_t cmp, uint32_t fixed_size)
{
    h_heap_st *hp = h_malloc(sizeof(h_heap_st));

    if(unlikely(hp == NULL))
        return NULL;

    bzero(hp, sizeof(h_heap_st));

    if (cmp)
        hp->__cmp = cmp;
    else
        hp->__cmp = default_cmp;

    if (fixed_size) {
        hp->__fixed = 1;
        hp->__size_base = fixed_size;

        hp->bases = h_malloc(fixed_size * sizeof(void *));
        if (!hp->bases) {
            h_free(hp);
            return NULL;
        }
    }
    return hp;
}
Пример #2
0
int input_alloc(input** target, window* window_target) {
	if (target == NULL) {
		debug_critical("[input_alloc] target cannot be NULL");
		return 0;
	}

	if (*target != NULL) {
		debug_warning("[input_alloc] target points to non NULL handle");
	}

	if (window_target == NULL) {
		debug_critical("[input_alloc] window_target cannot be NULL");
		return 0;

		/*
		 * NOTE : passing a window pointer is completely unnecessary and will never be used. we use this to force the user to have an existing window
		 * as a way of non-globally asserting that SDL has been initialized
		 */
	}

	*target = h_malloc(sizeof(input));

	input* this = *target;

	this->current_configuration_keyboard = INPUT_DEFAULT_KEYBOARD_CONFIG;
	this->current_configuration_controller = INPUT_DEFAULT_CONTROLLER_CONFIG;
	this->controller_deadzone = INPUT_DEFAULT_CONTROLLER_DEADZONE;
	this->controller_handle = NULL;
	this->controller_enabled = input_get_controller_connected(this);

	this->keyboard_state = SDL_GetKeyboardState(NULL);

	return 1;
}
Пример #3
0
void component_type_get_save_string(const component_type* type, void* component_data, char** out_save_buffer, int* out_save_buffer_length) {
	/* TODO: write parameter export function */

	const char* current_param_name = NULL;
	char current_param_value[1024] = {0};

	unsigned int current_index = 0;
	char* out_buffer = NULL;
	unsigned int out_buffer_length = 2048;

	out_buffer = h_malloc(out_buffer_length);
	memset(out_buffer, 0, out_buffer_length);

	strcat(out_buffer, type->component_name);
	strcat(out_buffer, " ");

	while (type->component_param_names[current_index]) {
		current_param_name = type->component_param_names[current_index];
		component_type_get_param_value(type, component_data, current_index, current_param_value, 1024);

		char current_override_string[1024] = {0};

		snprintf(current_override_string, 1024, "@%s=%s; ", current_param_name, current_param_value);

		strcat(out_buffer, current_override_string);

		current_index++;
	}

	*out_save_buffer = out_buffer;
	*out_save_buffer_length = out_buffer_length;
}
Пример #4
0
int sound_manager_alloc(sound_manager** target) {
	if (target == NULL) {
		debug_critical("[sound_manager_alloc] target cannot be NULL");
		return 0;
	}

	if (*target != NULL) {
		debug_warning("[sound_manager_alloc] target points to a non NULL handle, possible memory leak");
	}

	*target = h_malloc(sizeof(sound_manager));

	sound_manager* this = *target;

	this->system_handle = NULL;

	if (FMOD_System_Create(&this->system_handle) != FMOD_OK) {
		debug_critical("[sound_manager_alloc] FMOD failed to allocate sound system");
		h_free(*target);
		*target = NULL;
		return 0;
	}

	if (FMOD_System_Init(this->system_handle, HUNTER_SOUND_MANAGER_CHANNELS, 0, NULL) != FMOD_OK) {
		debug_critical("[sound_manager_alloc] failed to initialize sound system");
		h_free(*target);
		*target = NULL;
		return 0;
	}

	return 1;
}
Пример #5
0
int sound_alloc(sound** target, sound_manager* system, const char* filename, int streamed) {
	if (target == NULL) {
		debug_critical("[sound_alloc] target cannot be NULL");
		return 0;
	}

	if (*target != NULL) {
		debug_critical("[sound_alloc] target points to non NULL handle, possible memory leak");
	}

	*target = h_malloc(sizeof(sound));

	if (system == NULL) {
		debug_critical("[sound_alloc] system cannot be NULL");
		h_free(*target);
		*target = NULL;
	}

	sound* this = *target;
	memset(this, 0, sizeof(sound));

	this->sound_system = system->system_handle;

	if (FMOD_System_CreateSound(this->sound_system, filename, (streamed) ? FMOD_CREATESTREAM : FMOD_CREATESAMPLE, NULL, &(this->sound_object)) != FMOD_OK) {
		debug_message(filename);
		debug_critical("[sound_alloc] failed to initialize sound, check filename");
		return 0;
	}

	FMOD_Sound_SetMode(this->sound_object, FMOD_LOOP_NORMAL);

	return 1;
}
Пример #6
0
IOBuf *IOBuf_create(size_t len, int fd, IOBufType type)
{
    IOBuf *buf = h_calloc(sizeof(IOBuf), 1);
    check_mem(buf);

    buf->fd = fd;
    buf->len = len;

    buf->buf = h_malloc(len + 1);
    check_mem(buf->buf);

    hattach(buf->buf, buf);

    buf->type = type;

    if(type == IOBUF_SSL) {
        buf->use_ssl = 1;
        buf->handshake_performed = 0;
        ssl_init(&buf->ssl);
        ssl_set_endpoint(&buf->ssl, SSL_IS_SERVER);
        ssl_set_authmode(&buf->ssl, SSL_VERIFY_NONE);
        havege_init(&buf->hs);
        ssl_set_rng(&buf->ssl, havege_rand, &buf->hs);
        ssl_set_dbg(&buf->ssl, ssl_debug, NULL);
        ssl_set_bio(&buf->ssl, ssl_fdrecv_wrapper, buf, 
                    ssl_fdsend_wrapper, buf);
        ssl_set_session(&buf->ssl, 1, 0, &buf->ssn);
        memset(&buf->ssn, 0, sizeof(buf->ssn));

        buf->send = ssl_send;
        buf->recv = ssl_recv;
        buf->stream_file = ssl_stream_file;
    } else if(type == IOBUF_NULL) {
        buf->send = null_send;
        buf->recv = null_recv;
        buf->stream_file = null_stream_file;
    } else if(type == IOBUF_FILE) {
        buf->send = file_send;
        buf->recv = file_recv;
        buf->stream_file = plain_stream_file;
    } else if(type == IOBUF_SOCKET) {
        buf->send = plaintext_send;
        buf->recv = plaintext_recv;
        buf->stream_file = plain_stream_file;
    } else {
        sentinel("Invalid IOBufType given: %d", type);
    }

    return buf;

error:
    if(buf) h_free(buf);
    return NULL;
}
Пример #7
0
void* component_health_alloc(void) {
	component_health* output = h_malloc(sizeof(component_health));

	if (!output) {
		debug_critical("[component_health_alloc] memory allocation failure");
		return NULL;
	}

	memset(output, 0, sizeof(component_health));

	return output;
}
Пример #8
0
char *dns_pack_init()
{
    char *msg = NULL;
    msg = (char *)h_malloc(MAX_PACKET_SIZE);
    if (msg)
    {
        printf("create\n");
        return msg;
    }

    return NULL;
}
Пример #9
0
int framebuffer_alloc(framebuffer** target, unsigned int width, unsigned int height) {
	if (!target) {
		debug_critical("[framebuffer_alloc] target cannot be NULL");
		return 0;
	}

	if (*target) {
		debug_warning("[framebuffer_alloc] target points to non NULL handle, possible memory leak");
	}

	if (!width && !height) {
		width = window_get_width(global_get(global_get_singleton(), GLOBAL_WINDOW));
		height = window_get_height(global_get(global_get_singleton(), GLOBAL_WINDOW));
	}

	framebuffer* output = h_malloc(sizeof(framebuffer));

	glGenFramebuffers(1, &output->fb);
	glBindFramebuffer(GL_FRAMEBUFFER, output->fb);

	glGenTextures(1, &output->texture);
	glBindTexture(GL_TEXTURE_2D, output->texture);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

	glGenRenderbuffers(1, &output->renderbuffer);
	glBindRenderbuffer(GL_RENDERBUFFER, output->renderbuffer);

	glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
	glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, output->renderbuffer);

	glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, output->texture, 0);

	GLenum buffers[] = {GL_COLOR_ATTACHMENT0};
	glDrawBuffers(1, buffers);

	if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
		debug_critical("[framebuffer_alloc] failed to create framebuffer");
		return 0;
	}

	*target = output;

	return 1;
}
Пример #10
0
void* component_primitive_alloc(void) {
	component_primitive* output = h_malloc(sizeof(component_primitive));

	if (!output) {
		debug_critical("[component_primitive_alloc] memory allocation failure");
		return NULL;
	}

	memset(output, 0, sizeof(component_primitive));

	output->width = 1.0f;
	output->height = 1.0f;
	output->texwidth = 1.0f;
	output->texheight = 1.0f;
	output->primitive_type = PRIMITIVE_TYPE_RECT;

	primitive_alloc(&output->primitive_handle, output->primitive_type, output->width, output->height, output->texwidth, output->texheight);

	return output;
}
Пример #11
0
darray_t *darray_create(size_t element_size, size_t initial_max)
{
    darray_t *array = h_malloc(sizeof(darray_t));
    check_mem(array);

    array->contents = h_calloc(sizeof(void *), initial_max);
    check_mem(array->contents);
    hattach(array->contents, array);

    array->max = initial_max;
    array->end = 0;
    array->element_size = element_size;
    array->expand_rate = DEFAULT_EXPAND_RATE;

    return array;

error:
    if(array) h_free(array);
    return NULL;
}
Пример #12
0
void* alloc_component_music_trigger(char* buffer, int buffer_size) {
	if (!buffer) {
		debug_critical("[alloc_component_music_trigger] buffer cannot be NULL");
		return NULL;
	}

	component_music_trigger* output = h_malloc(sizeof(component_music_trigger));

	if (!output) {
		debug_critical("[alloc_component_music_trigger] memory allocation failure");
		return NULL;
	}

	memset(output, 0, sizeof(component_music_trigger));

	sound_map* sound_map_handle = global_get(global_get_singleton(), GLOBAL_SOUND_MAP);
	output->target_sound = sound_map_get(sound_map_handle, buffer);

	memcpy(output->original_sound, buffer, ((buffer_size > 255) ? 255 : buffer_size));

	return output;
}
Пример #13
0
int
serval_open_keyring(svl_crypto_ctx *ctx)
{
  CHECK_ERR(ctx,"Invalid ctx");
  if (ctx->keyring_len == 0) {
    // if no keyring specified, use default keyring
    CHECK(serval_path,"Default Serval path not initialized");
    char keyring_path_str[PATH_MAX] = {0};
    strcpy(keyring_path_str, serval_path);
    if (serval_path[strlen(serval_path) - 1] != '/')
      strcat(keyring_path_str, "/");
    strcat(keyring_path_str, "serval.keyring");
    // Fetching SAS keys requires setting the SERVALINSTANCE_PATH environment variable
    CHECK_ERR(setenv("SERVALINSTANCE_PATH", serval_path, 1) == 0,
	      "Failed to set SERVALINSTANCE_PATH env variable");
    ctx->keyring_len = strlen(keyring_path_str);
    ctx->keyring_path = h_malloc(ctx->keyring_len + 1);
    strcpy(ctx->keyring_path,keyring_path_str);
    hattach(ctx->keyring_path,ctx);
  } else {
    // otherwise, use specified keyring (NOTE: if keyring does not exist, it will be created)
    CHECK(ctx->keyring_len < PATH_MAX, "Keyring length too long");
  }
  
  ctx->keyring_file = keyring_open(ctx->keyring_path);
  CHECK_ERR(ctx->keyring_file, "Failed to open specified keyring file");

  if (keyring_enter_pin(ctx->keyring_file, KEYRING_PIN) <= 0) {
    // put initial identity in if we don't have any visible
    CHECK_ERR(keyring_seed(ctx->keyring_file) == 0, "Failed to seed keyring");
  }
  
  return 1;
error:
  return 0;
}
Пример #14
0
void component_texture_load_animset(component_texture* output, char* animset_filename) {
	for (int i = 0; i < COMPONENT_TEXTURE_MAX_ANIM_COUNT; i++) {
		for (int j = 0; j < output->frame_count[i]; j++) {
			if (!output->texture_buffer[i][j]) {
				continue;
			}

			texture_free(output->texture_buffer[i] + j);
		}
	}

	FILE* anim_set_file = fopen(animset_filename, "r");
	strcpy(output->texture_anim_setfile, animset_filename);

	if (!anim_set_file) {
		debug_critical("[component_texture_load_animset] cannot open anim set file %s", animset_filename);
		return;
	}

	/* Read the anim set file into the animation filename buffer. */

	char anim_filename_buffer[COMPONENT_TEXTURE_MAX_ANIM_COUNT][COMPONENT_TEXTURE_MAX_ANIM_SETFILE] = {{0}};

	unsigned int index = 0;

	while (fgets(anim_filename_buffer[index++], COMPONENT_TEXTURE_MAX_ANIM_SETFILE, anim_set_file)) {
		if (!strlen(anim_filename_buffer[index - 1])) {
			break;
		}

		char* target = anim_filename_buffer[index - 1];

		target[strcspn(target, "\n")] = 0;
	}

	fclose(anim_set_file);
	anim_set_file = NULL;

	for (int anim_id = 0; anim_id < index - 1; anim_id++) {
		/* loading animation files */
		FILE* file_handle = fopen(anim_filename_buffer[anim_id], "r");
	
		if (file_handle == NULL) {
			debug_critical("[component_texture_load_animset] failed to open animation file [%s]", anim_filename_buffer[anim_id]);
			return;
		}

		/* file is good, we start to load in the initial data */
		output->texture_anim_names[anim_id] = h_malloc(COMPONENT_TEXTURE_MAX_ANIM_NAME);
		memset(output->texture_anim_names[anim_id], 0, COMPONENT_TEXTURE_MAX_ANIM_NAME);
		fscanf(file_handle, "%s %d", output->texture_anim_names[anim_id], output->interval + anim_id);

		char texture_filename[COMPONENT_TEXTURE_MAX_FILENAME];
		memset(texture_filename, 0, COMPONENT_TEXTURE_MAX_FILENAME);

		const char* texture_filename_prefix = "resource/materials/";

		int i = 0;
		while (fscanf(file_handle, "%s", texture_filename) != EOF) {
			/* texture_filename should contain the next target texture */
			char* texture_filename_full = h_malloc(COMPONENT_TEXTURE_MAX_FILENAME + strlen(texture_filename_prefix));

			if (texture_filename_full == NULL) {
				debug_critical("[component_texture_load_animset] memory allocation failure");
				return;
			}

			memset(texture_filename_full, 0, COMPONENT_TEXTURE_MAX_FILENAME + strlen(texture_filename_prefix));

			memcpy(texture_filename_full, texture_filename_prefix, strlen(texture_filename_prefix));
			strcat(texture_filename_full, texture_filename);
			texture_alloc(output->texture_buffer[anim_id] + i++, texture_filename_full, 0, 0); // set the next texture, assume these will not be tiled
	
			memset(texture_filename, 0, COMPONENT_TEXTURE_MAX_FILENAME);
		}
	
		if (i == 0) {
			debug_critical("[component_texture_load_animset] invalid animation file (no textures given) [%s]", anim_filename_buffer[anim_id]);
			return;
		}
	
		output->frame_count[anim_id] = i;
		fclose(file_handle);
	}
}