コード例 #1
0
ファイル: buddy_test.c プロジェクト: txgcwm/code_study
int main(int argc, char** argv)
{
    struct buddy* buddy = buddy_create(32);

    buddy_dump(buddy);

    int mval = buddy_alloc(buddy, 4);
    buddy_dump(buddy);
    printf("malloc value: %d\n", mval);

    mval = buddy_alloc(buddy, 9);
    buddy_dump(buddy);
    printf("malloc value: %d\n", mval);

    mval = buddy_alloc(buddy, 3);
    buddy_dump(buddy);
    printf("malloc value: %d\n", mval);

    mval = buddy_alloc(buddy, 7);
    buddy_dump(buddy);
    printf("malloc value: %d\n", mval);

    buddy_destroy(buddy);

    return 0;
}
コード例 #2
0
/**
 * Simplify the command and call one of the sub parser functions
 *
 * @param cmd Raw command string. This parameter is mutated. This
 * parameter cannot be NULL.
 * @param cmd_len Length in bytes of the command string.
 * @return Program status.
 */
static status_t parse_command(char* cmd, int cmd_len)
{
	assert(cmd != NULL);

	int ws_cursor = 0;

	if (cmd[0] == '\0' || cmd[0] == '\n' || cmd[0] == '\r')
		return SUCCESS;

	// remove whitespace from command
	for (int i = 0; i < cmd_len; ++i) {
		switch (cmd[i]) {
		case ' ':
		case '\n':
		case '\r':
		case '\t':
			break;

		default:
			cmd[ws_cursor++] = cmd[i];
		}
	}

	status_t status;

	// We have 2 commands: alloc and free.
	if (strstr(cmd, "alloc") != NULL)
		status = parse_alloc(cmd);
	else if (strstr(cmd, "free") != NULL)
		status = parse_free(cmd);
	else
		return parse_error(cmd);

	if (status != SUCCESS)
		return status;

	// Output free blocks
	buddy_dump();

	return SUCCESS;
}