Converting to proper use of umem everywhere (kind of went off on a tanget there :D)

master
Allen Webster 2017-03-19 19:05:05 -04:00
parent c959566e67
commit fe7c0fe47e
29 changed files with 531 additions and 533 deletions

View File

@ -10,9 +10,9 @@ struct Application_Links;
#define GET_BUFFER_SIG(n) Buffer_Summary n(Application_Links *app, Buffer_ID buffer_id, Access_Flag access)
#define GET_BUFFER_BY_NAME_SIG(n) Buffer_Summary n(Application_Links *app, char *name, int32_t len, Access_Flag access)
#define BUFFER_READ_RANGE_SIG(n) bool32 n(Application_Links *app, Buffer_Summary *buffer, int32_t start, int32_t end, char *out)
#define BUFFER_REPLACE_RANGE_SIG(n) bool32 n(Application_Links *app, Buffer_Summary *buffer, int32_t start, int32_t end, char *str, int32_t len)
#define BUFFER_REPLACE_RANGE_SIG(n) bool32 n(Application_Links *app, Buffer_Summary *buffer, size_t start, size_t end, char *str, size_t len)
#define BUFFER_COMPUTE_CURSOR_SIG(n) bool32 n(Application_Links *app, Buffer_Summary *buffer, Buffer_Seek seek, Partial_Cursor *cursor_out)
#define BUFFER_BATCH_EDIT_SIG(n) bool32 n(Application_Links *app, Buffer_Summary *buffer, char *str, int32_t str_len, Buffer_Edit *edits, int32_t edit_count, Buffer_Batch_Edit_Type type)
#define BUFFER_BATCH_EDIT_SIG(n) bool32 n(Application_Links *app, Buffer_Summary *buffer, char *str, size_t str_len, Buffer_Edit *edits, uint32_t edit_count, Buffer_Batch_Edit_Type type)
#define BUFFER_ADD_MARKERS_SIG(n) Marker_Handle n(Application_Links *app, Buffer_Summary *buffer, uint32_t marker_count)
#define BUFFER_SET_MARKERS_SIG(n) bool32 n(Application_Links *app, Buffer_Summary *buffer, Marker_Handle marker, uint32_t first_marker_index, uint32_t marker_count, Marker *source_markers)
#define BUFFER_GET_MARKERS_SIG(n) bool32 n(Application_Links *app, Buffer_Summary *buffer, Marker_Handle marker, uint32_t first_marker_index, uint32_t marker_count, Marker *markers_out)
@ -60,9 +60,9 @@ struct Application_Links;
#define GET_FILE_LIST_SIG(n) File_List n(Application_Links *app, char *dir, int32_t len)
#define FREE_FILE_LIST_SIG(n) void n(Application_Links *app, File_List list)
#define SET_GUI_UP_DOWN_KEYS_SIG(n) void n(Application_Links *app, int16_t up_key, int16_t down_key)
#define MEMORY_ALLOCATE_SIG(n) void* n(Application_Links *app, int32_t size)
#define MEMORY_SET_PROTECTION_SIG(n) bool32 n(Application_Links *app, void *ptr, int32_t size, Memory_Protect_Flags flags)
#define MEMORY_FREE_SIG(n) void n(Application_Links *app, void *ptr, int32_t size)
#define MEMORY_ALLOCATE_SIG(n) void* n(Application_Links *app, size_t size)
#define MEMORY_SET_PROTECTION_SIG(n) bool32 n(Application_Links *app, void *ptr, size_t size, Memory_Protect_Flags flags)
#define MEMORY_FREE_SIG(n) void n(Application_Links *app, void *ptr, size_t size)
#define FILE_EXISTS_SIG(n) bool32 n(Application_Links *app, char *filename, int32_t len)
#define DIRECTORY_CD_SIG(n) bool32 n(Application_Links *app, char *dir, int32_t *len, int32_t capacity, char *rel_path, int32_t rel_len)
#define GET_4ED_PATH_SIG(n) int32_t n(Application_Links *app, char *out, int32_t capacity)
@ -378,9 +378,9 @@ static inline void get_buffer_next(Application_Links *app, Buffer_Summary *buffe
static inline Buffer_Summary get_buffer(Application_Links *app, Buffer_ID buffer_id, Access_Flag access){return(app->get_buffer(app, buffer_id, access));}
static inline Buffer_Summary get_buffer_by_name(Application_Links *app, char *name, int32_t len, Access_Flag access){return(app->get_buffer_by_name(app, name, len, access));}
static inline bool32 buffer_read_range(Application_Links *app, Buffer_Summary *buffer, int32_t start, int32_t end, char *out){return(app->buffer_read_range(app, buffer, start, end, out));}
static inline bool32 buffer_replace_range(Application_Links *app, Buffer_Summary *buffer, int32_t start, int32_t end, char *str, int32_t len){return(app->buffer_replace_range(app, buffer, start, end, str, len));}
static inline bool32 buffer_replace_range(Application_Links *app, Buffer_Summary *buffer, size_t start, size_t end, char *str, size_t len){return(app->buffer_replace_range(app, buffer, start, end, str, len));}
static inline bool32 buffer_compute_cursor(Application_Links *app, Buffer_Summary *buffer, Buffer_Seek seek, Partial_Cursor *cursor_out){return(app->buffer_compute_cursor(app, buffer, seek, cursor_out));}
static inline bool32 buffer_batch_edit(Application_Links *app, Buffer_Summary *buffer, char *str, int32_t str_len, Buffer_Edit *edits, int32_t edit_count, Buffer_Batch_Edit_Type type){return(app->buffer_batch_edit(app, buffer, str, str_len, edits, edit_count, type));}
static inline bool32 buffer_batch_edit(Application_Links *app, Buffer_Summary *buffer, char *str, size_t str_len, Buffer_Edit *edits, uint32_t edit_count, Buffer_Batch_Edit_Type type){return(app->buffer_batch_edit(app, buffer, str, str_len, edits, edit_count, type));}
static inline Marker_Handle buffer_add_markers(Application_Links *app, Buffer_Summary *buffer, uint32_t marker_count){return(app->buffer_add_markers(app, buffer, marker_count));}
static inline bool32 buffer_set_markers(Application_Links *app, Buffer_Summary *buffer, Marker_Handle marker, uint32_t first_marker_index, uint32_t marker_count, Marker *source_markers){return(app->buffer_set_markers(app, buffer, marker, first_marker_index, marker_count, source_markers));}
static inline bool32 buffer_get_markers(Application_Links *app, Buffer_Summary *buffer, Marker_Handle marker, uint32_t first_marker_index, uint32_t marker_count, Marker *markers_out){return(app->buffer_get_markers(app, buffer, marker, first_marker_index, marker_count, markers_out));}
@ -428,9 +428,9 @@ static inline int32_t directory_get_hot(Application_Links *app, char *out, int32
static inline File_List get_file_list(Application_Links *app, char *dir, int32_t len){return(app->get_file_list(app, dir, len));}
static inline void free_file_list(Application_Links *app, File_List list){(app->free_file_list(app, list));}
static inline void set_gui_up_down_keys(Application_Links *app, int16_t up_key, int16_t down_key){(app->set_gui_up_down_keys(app, up_key, down_key));}
static inline void* memory_allocate(Application_Links *app, int32_t size){return(app->memory_allocate(app, size));}
static inline bool32 memory_set_protection(Application_Links *app, void *ptr, int32_t size, Memory_Protect_Flags flags){return(app->memory_set_protection(app, ptr, size, flags));}
static inline void memory_free(Application_Links *app, void *ptr, int32_t size){(app->memory_free(app, ptr, size));}
static inline void* memory_allocate(Application_Links *app, size_t size){return(app->memory_allocate(app, size));}
static inline bool32 memory_set_protection(Application_Links *app, void *ptr, size_t size, Memory_Protect_Flags flags){return(app->memory_set_protection(app, ptr, size, flags));}
static inline void memory_free(Application_Links *app, void *ptr, size_t size){(app->memory_free(app, ptr, size));}
static inline bool32 file_exists(Application_Links *app, char *filename, int32_t len){return(app->file_exists(app, filename, len));}
static inline bool32 directory_cd(Application_Links *app, char *dir, int32_t *len, int32_t capacity, char *rel_path, int32_t rel_len){return(app->directory_cd(app, dir, len, capacity, rel_path, rel_len));}
static inline int32_t get_4ed_path(Application_Links *app, char *out, int32_t capacity){return(app->get_4ed_path(app, out, capacity));}
@ -450,9 +450,9 @@ static inline void get_buffer_next(Application_Links *app, Buffer_Summary *buffe
static inline Buffer_Summary get_buffer(Application_Links *app, Buffer_ID buffer_id, Access_Flag access){return(app->get_buffer_(app, buffer_id, access));}
static inline Buffer_Summary get_buffer_by_name(Application_Links *app, char *name, int32_t len, Access_Flag access){return(app->get_buffer_by_name_(app, name, len, access));}
static inline bool32 buffer_read_range(Application_Links *app, Buffer_Summary *buffer, int32_t start, int32_t end, char *out){return(app->buffer_read_range_(app, buffer, start, end, out));}
static inline bool32 buffer_replace_range(Application_Links *app, Buffer_Summary *buffer, int32_t start, int32_t end, char *str, int32_t len){return(app->buffer_replace_range_(app, buffer, start, end, str, len));}
static inline bool32 buffer_replace_range(Application_Links *app, Buffer_Summary *buffer, size_t start, size_t end, char *str, size_t len){return(app->buffer_replace_range_(app, buffer, start, end, str, len));}
static inline bool32 buffer_compute_cursor(Application_Links *app, Buffer_Summary *buffer, Buffer_Seek seek, Partial_Cursor *cursor_out){return(app->buffer_compute_cursor_(app, buffer, seek, cursor_out));}
static inline bool32 buffer_batch_edit(Application_Links *app, Buffer_Summary *buffer, char *str, int32_t str_len, Buffer_Edit *edits, int32_t edit_count, Buffer_Batch_Edit_Type type){return(app->buffer_batch_edit_(app, buffer, str, str_len, edits, edit_count, type));}
static inline bool32 buffer_batch_edit(Application_Links *app, Buffer_Summary *buffer, char *str, size_t str_len, Buffer_Edit *edits, uint32_t edit_count, Buffer_Batch_Edit_Type type){return(app->buffer_batch_edit_(app, buffer, str, str_len, edits, edit_count, type));}
static inline Marker_Handle buffer_add_markers(Application_Links *app, Buffer_Summary *buffer, uint32_t marker_count){return(app->buffer_add_markers_(app, buffer, marker_count));}
static inline bool32 buffer_set_markers(Application_Links *app, Buffer_Summary *buffer, Marker_Handle marker, uint32_t first_marker_index, uint32_t marker_count, Marker *source_markers){return(app->buffer_set_markers_(app, buffer, marker, first_marker_index, marker_count, source_markers));}
static inline bool32 buffer_get_markers(Application_Links *app, Buffer_Summary *buffer, Marker_Handle marker, uint32_t first_marker_index, uint32_t marker_count, Marker *markers_out){return(app->buffer_get_markers_(app, buffer, marker, first_marker_index, marker_count, markers_out));}
@ -500,9 +500,9 @@ static inline int32_t directory_get_hot(Application_Links *app, char *out, int32
static inline File_List get_file_list(Application_Links *app, char *dir, int32_t len){return(app->get_file_list_(app, dir, len));}
static inline void free_file_list(Application_Links *app, File_List list){(app->free_file_list_(app, list));}
static inline void set_gui_up_down_keys(Application_Links *app, int16_t up_key, int16_t down_key){(app->set_gui_up_down_keys_(app, up_key, down_key));}
static inline void* memory_allocate(Application_Links *app, int32_t size){return(app->memory_allocate_(app, size));}
static inline bool32 memory_set_protection(Application_Links *app, void *ptr, int32_t size, Memory_Protect_Flags flags){return(app->memory_set_protection_(app, ptr, size, flags));}
static inline void memory_free(Application_Links *app, void *ptr, int32_t size){(app->memory_free_(app, ptr, size));}
static inline void* memory_allocate(Application_Links *app, size_t size){return(app->memory_allocate_(app, size));}
static inline bool32 memory_set_protection(Application_Links *app, void *ptr, size_t size, Memory_Protect_Flags flags){return(app->memory_set_protection_(app, ptr, size, flags));}
static inline void memory_free(Application_Links *app, void *ptr, size_t size){(app->memory_free_(app, ptr, size));}
static inline bool32 file_exists(Application_Links *app, char *filename, int32_t len){return(app->file_exists_(app, filename, len));}
static inline bool32 directory_cd(Application_Links *app, char *dir, int32_t *len, int32_t capacity, char *rel_path, int32_t rel_len){return(app->directory_cd_(app, dir, len, capacity, rel_path, rel_len));}
static inline int32_t get_4ed_path(Application_Links *app, char *out, int32_t capacity){return(app->get_4ed_path_(app, out, capacity));}

View File

@ -494,7 +494,7 @@ STRUCT Buffer_Seek{
UNION{
STRUCT {
/* DOC(The pos field specified the pos when the seek is in absolute position.) */
int32_t pos;
size_t pos;
};
STRUCT {
/* DOC(For xy coordinate seeks, rounding down means that any x in the box of the character lands on that character. For instance when clicking rounding down is the user's expected behavior. Not rounding down means that the right hand portion of the character's box, which is closer to the next character, will land on that next character. The unrounded behavior is the expected behavior when moving vertically and keeping the preferred x.) */
@ -506,9 +506,9 @@ STRUCT Buffer_Seek{
};
STRUCT {
/* DOC(The line number of a line-character type seek.) */
int32_t line;
size_t line;
/* DOC(The character number of a line-character type seek.) */
int32_t character;
size_t character;
};
};
};
@ -517,15 +517,15 @@ STRUCT Buffer_Seek{
DOC_SEE(4coder_Buffer_Positioning_System) */
STRUCT Full_Cursor{
/* DOC(This field contains the cursor's position in absolute byte index positioning.) */
int32_t pos;
size_t pos;
/* DOC(This field contains the cursor's position in apparent character index positioning.) */
int32_t character_pos;
size_t character_pos;
/* DOC(This field contains the number of the line where the cursor is located. This field is one based.) */
int32_t line;
size_t line;
/* DOC(This field contains the number of the character from the beginninf of the line where the cursor is located. This field is one based.) */
int32_t character;
size_t character;
/* DOC(This field contains the number of the line where the cursor is located, taking the line wrapping into account. This field is one based.) */
int32_t wrap_line;
size_t wrap_line;
/* DOC(This field contains the x position measured with unwrapped lines.) */
float unwrapped_x;
/* DOC(This field contains the y position measured with unwrapped lines.) */
@ -540,24 +540,24 @@ STRUCT Full_Cursor{
DOC_SEE(4coder_Buffer_Positioning_System) */
STRUCT Partial_Cursor{
/* DOC(This field contains the cursor's position in absolute byte index positioning.) */
int32_t pos;
size_t pos;
/* DOC(This field contains the number of the character from the beginninf of the line
where the cursor is located. This field is one based.) */
int32_t line;
size_t line;
/* DOC(This field contains the number of the column where the cursor is located. This field is one based.) */
int32_t character;
size_t character;
};
/* DOC(Buffer_Edit describes a range of a buffer and string to replace that range. A Buffer_Edit has to be paired with a string that contains the actual text that will be replaced into the buffer.) */
STRUCT Buffer_Edit{
/* DOC(The str_start field specifies the first character in the accompanying string that corresponds with this edit.) */
int32_t str_start;
size_t str_start;
/* DOC(The len field specifies the length of the string being written into the buffer.) */
int32_t len;
size_t len;
/* DOC(The start field specifies the start of the range in the buffer to replace in absolute position.) */
int32_t start;
size_t start;
/* DOC(The end field specifies one past the end of the range in the buffer to replace in absolute position.) */
int32_t end;
size_t end;
};
/* DOC(Buffer_Summary acts as a handle to a buffer and describes the state of the buffer.)
@ -635,6 +635,15 @@ STRUCT i32_Rect{
GLOBAL_VAR i32_Rect null_i32_rect = {0};
STRUCT pos_Rect{
size_t x0;
size_t y0;
size_t x1;
size_t y1;
};
GLOBAL_VAR pos_Rect null_pos_rect = {0};
STRUCT f32_Rect{
float x0;
float y0;

View File

@ -323,7 +323,7 @@ get_indentation_marks(Application_Links *app, Partition *part, Buffer_Summary *b
line_index = line_start;
}
int32_t next_line_start_pos = buffer_get_line_start(app, buffer, line_index+1);
uint32_t next_line_start_pos = buffer_get_line_start(app, buffer, line_index+1);
switch (token_ptr->type){
case CPP_TOKEN_BRACKET_OPEN: indent.current_indent += tab_width; break;
@ -353,8 +353,8 @@ get_indentation_marks(Application_Links *app, Partition *part, Buffer_Summary *b
{
int32_t previous_indent = indent.previous_line_indent;
int32_t this_line_start = buffer_get_line_start(app, buffer, line_index);
int32_t next_line_start = buffer_get_line_start(app, buffer, line_index+1);
uint32_t this_line_start = buffer_get_line_start(app, buffer, line_index);
uint32_t next_line_start = buffer_get_line_start(app, buffer, line_index+1);
bool32 did_special_behavior = false;
@ -523,9 +523,9 @@ get_indent_lines_whole_tokens(Application_Links *app, Buffer_Summary *buffer, Cp
int32_t line_end = buffer_get_line_index(app, buffer, end_pos);
for (;line_start > 0;){
int32_t line_start_pos = 0;
uint32_t line_start_pos = 0;
Cpp_Token *token = get_first_token_at_line(app, buffer, tokens, line_start, &line_start_pos);
if (token && token->start < line_start_pos){
if (token != 0 && token->start < line_start_pos){
line_start = buffer_get_line_index(app, buffer, token->start);
}
else{
@ -534,7 +534,7 @@ get_indent_lines_whole_tokens(Application_Links *app, Buffer_Summary *buffer, Cp
}
for (;line_end+1 < buffer->line_count;){
int32_t next_line_start_pos = 0;
uint32_t next_line_start_pos = 0;
Cpp_Token *token = get_first_token_at_line(app, buffer, tokens, line_end+1, &next_line_start_pos);
if (token && token->start < next_line_start_pos){
line_end = buffer_get_line_index(app, buffer, token->start+token->size);

View File

@ -322,16 +322,16 @@ struct Config_Item{
struct Config_Array_Reader{
Cpp_Token_Array array;
char *mem;
int32_t i;
int32_t val_array_end;
uint32_t i;
uint32_t val_array_end;
bool32 good;
};
static Cpp_Token
read_config_token(Cpp_Token_Array array, int32_t *i_ptr){
read_config_token(Cpp_Token_Array array, uint32_t *i_ptr){
Cpp_Token token = {0};
int32_t i = *i_ptr;
uint32_t i = *i_ptr;
for (; i < array.count; ++i){
Cpp_Token comment_token = array.tokens[i];
@ -350,10 +350,10 @@ read_config_token(Cpp_Token_Array array, int32_t *i_ptr){
}
static Config_Line
read_config_line(Cpp_Token_Array array, int32_t *i_ptr){
read_config_line(Cpp_Token_Array array, uint32_t *i_ptr){
Config_Line config_line = {0};
int32_t i = *i_ptr;
uint32_t i = *i_ptr;
config_line.id_token = read_config_token(array, &i);
if (config_line.id_token.type == CPP_TOKEN_IDENTIFIER){
@ -723,7 +723,7 @@ process_config_file(Application_Links *app){
int32_t new_wrap_width = default_wrap_width;
int32_t new_min_base_width = default_min_base_width;
for (int32_t i = 0; i < array.count; ++i){
for (uint32_t i = 0; i < array.count; ++i){
Config_Line config_line = read_config_line(array, &i);
if (config_line.read_success){

View File

@ -59,8 +59,8 @@ list_all_functions(Application_Links *app, Partition *part, Buffer_Summary *buff
Partition extra_memory_ = partition_sub_part(part, (4<<10));
Partition *extra_memory = &extra_memory_;
char *str = (char*)partition_current(part);
int32_t part_size = 0;
int32_t size = 0;
size_t part_size = 0;
size_t size = 0;
static const int32_t token_chunk_size = 512;
Cpp_Token token_chunk[token_chunk_size];
@ -217,7 +217,7 @@ list_all_functions(Application_Links *app, Partition *part, Buffer_Summary *buff
}
if (delete_space_before){
int32_t pos = extra_memory->pos - 1;
size_t pos = extra_memory->pos - 1;
char *base = ((char*)(extra_memory->base));
if (pos >= 0 && base[pos] == ' '){
extra_memory->pos = pos;
@ -242,13 +242,13 @@ list_all_functions(Application_Links *app, Partition *part, Buffer_Summary *buff
finish_print:;
{
int32_t sig_size = extra_memory->pos;
String sig = make_string(extra_memory->base, sig_size);
size_t sig_size = extra_memory->pos;
String sig = make_string(extra_memory->base, (int32_t)sig_size);
int32_t line_number = buffer_get_line_index(app, buffer, open_paren_pos);
int32_t line_number_len = int_to_str_size(line_number);
int32_t append_len = buffer_name.size + 1 + line_number_len + 1 + 1 + sig_size + 1;
size_t append_len = buffer_name.size + 1 + line_number_len + 1 + 1 + sig_size + 1;
char *out_space = push_array(part, char, append_len);
if (out_space == 0){
@ -263,7 +263,7 @@ list_all_functions(Application_Links *app, Partition *part, Buffer_Summary *buff
}
part_size += append_len;
String out = make_string(out_space, 0, append_len);
String out = make_string(out_space, 0, (int32_t)append_len);
append(&out, buffer_name);
append(&out, ':');
append_int_to_str(&out, line_number);

View File

@ -212,9 +212,9 @@ adjust_all_buffer_wrap_widths(Application_Links *app, int32_t wrap_width, int32_
}
// TODO(allen): Setup buffer seeking to do character_pos and get View_Summary out of this parameter list.
static int32_t
character_pos_to_pos(Application_Links *app, View_Summary *view, Buffer_Summary *buffer, int32_t character_pos){
int32_t result = 0;
static size_t
character_pos_to_pos(Application_Links *app, View_Summary *view, Buffer_Summary *buffer, size_t character_pos){
size_t result = 0;
Full_Cursor cursor = {0};
if (view_compute_cursor(app, view, seek_character_pos(character_pos), &cursor)){
result = cursor.pos;
@ -223,8 +223,8 @@ character_pos_to_pos(Application_Links *app, View_Summary *view, Buffer_Summary
}
struct Buffer_Rect{
int32_t char0, line0;
int32_t char1, line1;
size_t char0, line0;
size_t char1, line1;
};
#ifndef Swap
@ -242,26 +242,26 @@ get_rect(View_Summary *view){
rect.line1 = view->cursor.line;
if (rect.line0 > rect.line1){
Swap(int32_t, rect.line0, rect.line1);
Swap(size_t, rect.line0, rect.line1);
}
if (rect.char0 > rect.char1){
Swap(int32_t, rect.char0, rect.char1);
Swap(size_t, rect.char0, rect.char1);
}
return(rect);
}
static i32_Rect
static pos_Rect
get_line_x_rect(View_Summary *view){
i32_Rect rect = {0};
pos_Rect rect = {0};
if (view->unwrapped_lines){
rect.x0 = (int32_t)view->mark.unwrapped_x;
rect.x1 = (int32_t)view->cursor.unwrapped_x;
rect.x0 = (size_t)view->mark.unwrapped_x;
rect.x1 = (size_t)view->cursor.unwrapped_x;
}
else{
rect.x0 = (int32_t)view->mark.wrapped_x;
rect.x1 = (int32_t)view->cursor.wrapped_x;
rect.x0 = (size_t)view->mark.wrapped_x;
rect.x1 = (size_t)view->cursor.wrapped_x;
}
rect.y0 = view->mark.line;
rect.y1 = view->cursor.line;

View File

@ -464,7 +464,7 @@ buffer_seek_alphanumeric_or_camel_left(Application_Links *app, Buffer_Summary *b
}
static int32_t
seek_token_left(Cpp_Token_Array *tokens, int32_t pos){
seek_token_left(Cpp_Token_Array *tokens, uint32_t pos){
Cpp_Get_Token_Result get = cpp_get_token(*tokens, pos);
if (get.token_index == -1){
get.token_index = 0;
@ -479,16 +479,18 @@ seek_token_left(Cpp_Token_Array *tokens, int32_t pos){
}
static int32_t
seek_token_right(Cpp_Token_Array *tokens, int32_t pos){
seek_token_right(Cpp_Token_Array *tokens, uint32_t pos){
Cpp_Get_Token_Result get = cpp_get_token(*tokens, pos);
if (get.in_whitespace){
++get.token_index;
}
if (get.token_index >= tokens->count){
get.token_index = tokens->count-1;
uint32_t target_token_index = (uint32_t)(get.token_index);
if (target_token_index >= tokens->count){
target_token_index = tokens->count-1;
}
Cpp_Token *token = tokens->tokens + get.token_index;
Cpp_Token *token = tokens->tokens + target_token_index;
return(token->start + token->size);
}
@ -966,21 +968,22 @@ buffer_get_line_index(Application_Links *app, Buffer_Summary *buffer, int32_t po
}
static Cpp_Token*
get_first_token_at_line(Application_Links *app, Buffer_Summary *buffer, Cpp_Token_Array tokens, int32_t line, int32_t *line_start_out = 0){
get_first_token_at_line(Application_Links *app, Buffer_Summary *buffer, Cpp_Token_Array tokens, int32_t line, uint32_t *line_start_out = 0){
int32_t line_start = buffer_get_line_start(app, buffer, line);
Cpp_Get_Token_Result get_token = cpp_get_token(tokens, line_start);
Cpp_Get_Token_Result get = cpp_get_token(tokens, line_start);
if (get_token.in_whitespace){
get_token.token_index += 1;
if (get.in_whitespace){
get.token_index += 1;
}
uint32_t target_token_index = (uint32_t)(get.token_index);
if (line_start_out){
*line_start_out = line_start;
}
Cpp_Token *result = 0;
if (get_token.token_index < tokens.count){
result = tokens.tokens + get_token.token_index;
if (target_token_index < tokens.count){
result = tokens.tokens + target_token_index;
}
return(result);

View File

@ -13,7 +13,7 @@
#define FRED_BUFFER_TYPES_H
static Buffer_Seek
seek_pos(int32_t pos){
seek_pos(size_t pos){
Buffer_Seek result;
result.type = buffer_seek_pos;
result.pos = pos;
@ -21,7 +21,7 @@ seek_pos(int32_t pos){
}
static Buffer_Seek
seek_character_pos(int32_t pos){
seek_character_pos(size_t pos){
Buffer_Seek result;
result.type = buffer_seek_character_pos;
result.pos = pos;
@ -29,7 +29,7 @@ seek_character_pos(int32_t pos){
}
static Buffer_Seek
seek_wrapped_xy(float x, float y, int32_t round_down){
seek_wrapped_xy(float x, float y, bool32 round_down){
Buffer_Seek result;
result.type = buffer_seek_wrapped_xy;
result.x = x;
@ -39,7 +39,7 @@ seek_wrapped_xy(float x, float y, int32_t round_down){
}
static Buffer_Seek
seek_unwrapped_xy(float x, float y, int32_t round_down){
seek_unwrapped_xy(float x, float y, bool32 round_down){
Buffer_Seek result;
result.type = buffer_seek_unwrapped_xy;
result.x = x;
@ -59,7 +59,7 @@ seek_xy(float x, float y, bool32 round_down, bool32 unwrapped){
}
static Buffer_Seek
seek_line_char(int32_t line, int32_t character){
seek_line_char(size_t line, size_t character){
Buffer_Seek result;
result.type = buffer_seek_line_char;
result.line = line;

View File

@ -44,23 +44,23 @@ typedef int32_t b32_4tech;
struct Partition{
char *base;
i32_4tech pos;
i32_4tech max;
umem_4tech pos;
umem_4tech max;
};
struct Temp_Memory{
void *handle;
i32_4tech pos;
umem_4tech pos;
};
struct Tail_Temp_Partition{
Partition part;
void *handle;
i32_4tech old_max;
umem_4tech old_max;
};
inline Partition
make_part(void *memory, i32_4tech size){
make_part(void *memory, umem_4tech size){
Partition partition;
partition.base = (char*)memory;
partition.pos = 0;
@ -69,7 +69,7 @@ make_part(void *memory, i32_4tech size){
}
inline void*
partition_allocate(Partition *data, i32_4tech size){
partition_allocate(Partition *data, umem_4tech size){
void *ret = 0;
if (size > 0 && data->pos + size <= data->max){
ret = data->base + data->pos;
@ -79,7 +79,7 @@ partition_allocate(Partition *data, i32_4tech size){
}
inline void
partition_align(Partition *data, u32_4tech boundary){
partition_align(Partition *data, umem_4tech boundary){
--boundary;
data->pos = (data->pos + boundary) & (~boundary);
}
@ -89,7 +89,7 @@ partition_current(Partition *data){
return(data->base + data->pos);
}
inline i32_4tech
inline umem_4tech
partition_remaining(Partition *data){
return(data->max - data->pos);
}
@ -122,7 +122,7 @@ end_temp_memory(Temp_Memory temp){
}
inline Tail_Temp_Partition
begin_tail_part(Partition *data, i32_4tech size){
begin_tail_part(Partition *data, umem_4tech size){
Tail_Temp_Partition result = {0};
if (data->pos + size <= data->max){
result.handle = data;

View File

@ -50,7 +50,7 @@ static void
close_all_files_with_extension(Application_Links *app, Partition *scratch_part, char **extension_list, int32_t extension_count){
Temp_Memory temp = begin_temp_memory(scratch_part);
int32_t buffers_to_close_max = partition_remaining(scratch_part)/sizeof(int32_t);
size_t buffers_to_close_max = partition_remaining(scratch_part)/sizeof(int32_t);
int32_t *buffers_to_close = push_array(scratch_part, int32_t, buffers_to_close_max);
int32_t buffers_to_close_count = 0;
@ -225,7 +225,7 @@ CUSTOM_COMMAND_SIG(load_project){
}
// Read the settings from project.4coder
for (int32_t i = 0; i < array.count; ++i){
for (uint32_t i = 0; i < array.count; ++i){
Config_Line config_line = read_config_line(array, &i);
if (config_line.read_success){
Config_Item item = get_config_item(config_line, mem, array);

View File

@ -42,6 +42,9 @@ typedef int32_t b32_4tech;
#endif
// standard preamble end
#include "4cpp_lexer_types.h"
#include "4cpp_lexer_tables.c"
// duff-routine defines
#define DfrCase(PC) case PC: goto resumespot_##PC
#define DfrYield(PC, n) { *S_ptr = S; S_ptr->__pc__ = PC; return(n); resumespot_##PC:; }
@ -51,9 +54,6 @@ typedef int32_t b32_4tech;
# define FCPP_LINK static
#endif
#include "4cpp_lexer_types.h"
#include "4cpp_lexer_tables.c"
// TODO(allen): revisit this keyword data declaration system
struct String_And_Flag{
char *str;
@ -191,7 +191,7 @@ static String_And_Flag keywords[] = {
static i32_4tech keywords_count = sizeof(keywords)/sizeof(keywords[0]);
API_EXPORT FCPP_LINK Cpp_Get_Token_Result
cpp_get_token(Cpp_Token_Array array, i32_4tech pos)/*
cpp_get_token(Cpp_Token_Array array, u32_4tech pos)/*
DOC_PARAM(array, The array of tokens from which to get a token.)
DOC_PARAM(pos, The position, measured in bytes, to get the token for.)
DOC_RETURN(A Cpp_Get_Token_Result struct is returned containing the index of a token and a flag indicating whether the pos is contained in the token or in whitespace after the token.)
@ -206,14 +206,14 @@ DOC_SEE(Cpp_Get_Token_Result)
i32_4tech first = 0;
i32_4tech count = array.count;
i32_4tech last = count;
i32_4tech this_start = 0, next_start = 0;
if (count > 0){
for (;;){
result.token_index = (first + last)/2;
token = token_array + result.token_index;
this_start = token->start;
u32_4tech this_start = token->start;
u32_4tech next_start = 0;
if (result.token_index + 1 < count){
next_start = (token + 1)->start;
@ -221,6 +221,7 @@ DOC_SEE(Cpp_Get_Token_Result)
else{
next_start = this_start + token->size;
}
if (this_start <= pos && pos < next_start){
break;
}
@ -230,6 +231,7 @@ DOC_SEE(Cpp_Get_Token_Result)
else{
first = result.token_index + 1;
}
if (first == last){
result.token_index = first;
break;
@ -307,7 +309,7 @@ cpp_pp_directive_to_state(Cpp_Token_Type type){
}
FCPP_LINK b32_4tech
cpp__match(char *a, i32_4tech a_len, char *b, i32_4tech b_len){
cpp__match(char *a, u32_4tech a_len, char *b, u32_4tech b_len){
b32_4tech result = false;
if (a_len == b_len){
char *a_end = a + a_len;
@ -323,7 +325,7 @@ cpp__match(char *a, i32_4tech a_len, char *b, i32_4tech b_len){
}
FCPP_LINK b32_4tech
cpp__table_match(String_And_Flag *table, i32_4tech count, char *s, i32_4tech len, i32_4tech *index_out){
cpp__table_match(String_And_Flag *table, i32_4tech count, char *s, u32_4tech len, i32_4tech *index_out){
b32_4tech result = false;
String_And_Flag *entry = table;
*index_out = -1;
@ -338,7 +340,7 @@ cpp__table_match(String_And_Flag *table, i32_4tech count, char *s, i32_4tech len
}
FCPP_LINK Cpp_Lex_Result
cpp_lex_nonalloc_null_end_no_limit(Cpp_Lex_Data *S_ptr, char *chunk, i32_4tech size, Cpp_Token_Array *token_array_out){
cpp_lex_nonalloc_null_end_no_limit(Cpp_Lex_Data *S_ptr, char *chunk, u32_4tech size, Cpp_Token_Array *token_array_out){
Cpp_Lex_Data S = *S_ptr;
Cpp_Token *out_tokens = token_array_out->tokens;
@ -347,7 +349,7 @@ cpp_lex_nonalloc_null_end_no_limit(Cpp_Lex_Data *S_ptr, char *chunk, i32_4tech s
u8_4tech c = 0;
i32_4tech end_pos = size + S.chunk_pos;
u32_4tech end_pos = size + S.chunk_pos;
chunk -= S.chunk_pos;
switch (S.__pc__){
@ -968,7 +970,7 @@ cpp_lex_nonalloc_null_end_out_limit(Cpp_Lex_Data *S_ptr, char *chunk, i32_4tech
}
FCPP_LINK Cpp_Lex_Result
cpp_lex_nonalloc_no_null_no_limit(Cpp_Lex_Data *S_ptr, char *chunk, i32_4tech size, i32_4tech full_size,
cpp_lex_nonalloc_no_null_no_limit(Cpp_Lex_Data *S_ptr, char *chunk, u32_4tech size, u32_4tech full_size,
Cpp_Token_Array *token_array_out){
Cpp_Lex_Result result = 0;
if (S_ptr->pos >= full_size){
@ -1139,7 +1141,7 @@ cpp_shift_token_starts(Cpp_Token_Array *array, i32_4tech from_token_i, i32_4tech
}
FCPP_LINK Cpp_Token
cpp_index_array(Cpp_Token_Array *array, i32_4tech file_size, i32_4tech index){
cpp_index_array(Cpp_Token_Array *array, i32_4tech file_size, u32_4tech index){
Cpp_Token result;
if (index < array->count){
result = array->tokens[index];
@ -1155,16 +1157,11 @@ cpp_index_array(Cpp_Token_Array *array, i32_4tech file_size, i32_4tech index){
}
API_EXPORT FCPP_LINK Cpp_Relex_Range
cpp_get_relex_range(Cpp_Token_Array *array, i32_4tech start_pos, i32_4tech end_pos)
cpp_get_relex_range(Cpp_Token_Array *array, u32_4tech start_pos, u32_4tech end_pos)
/*
DOC_PARAM(array, A pointer to the token array that will be modified by the relex,
this array should already contain the tokens for the previous state of the file.)
DOC_PARAM(start_pos, The start position of the edited region of the file.
The start and end points are based on the edited region of the file before the edit.)
DOC_PARAM(end_pos, The end position of the edited region of the file.
In particular, end_pos is the first character after the edited region not effected by the edit.
Thus if the edited region contained one character end_pos - start_pos should equal 1.
The start and end points are based on the edited region of the file before the edit.)
DOC_PARAM(array, A pointer to the token array that will be modified by the relex, this array should already contain the tokens for the previous state of the file.)
DOC_PARAM(start_pos, The start position of the edited region of the file. The start and end points are based on the edited region of the file before the edit.)
DOC_PARAM(end_pos, The end position of the edited region of the file. In particular, end_pos is the first character after the edited region not effected by the edit. Thus if the edited region contained one character end_pos - start_pos should equal 1. The start and end points are based on the edited region of the file before the edit.)
*/{
Cpp_Relex_Range range = {0};
Cpp_Get_Token_Result get_result = {0};
@ -1188,27 +1185,18 @@ The start and end points are based on the edited region of the file before the e
}
API_EXPORT FCPP_LINK Cpp_Relex_Data
cpp_relex_init(Cpp_Token_Array *array, i32_4tech start_pos, i32_4tech end_pos, i32_4tech character_shift_amount)
cpp_relex_init(Cpp_Token_Array *array, u32_4tech start_pos, u32_4tech end_pos, i32_4tech character_shift_amount)
/*
DOC_PARAM(array, A pointer to the token array that will be modified by the relex,
this array should already contain the tokens for the previous state of the file.)
DOC_PARAM(start_pos, The start position of the edited region of the file.
The start and end points are based on the edited region of the file before the edit.)
DOC_PARAM(end_pos, The end position of the edited region of the file.
In particular, end_pos is the first character after the edited region not effected by the edit.
Thus if the edited region contained one character end_pos - start_pos should equal 1.
The start and end points are based on the edited region of the file before the edit.)
DOC_PARAM(array, A pointer to the token array that will be modified by the relex, this array should already contain the tokens for the previous state of the file.)
DOC_PARAM(start_pos, The start position of the edited region of the file. The start and end points are based on the edited region of the file before the edit.)
DOC_PARAM(end_pos, The end position of the edited region of the file. In particular, end_pos is the first character after the edited region not effected by the edit. Thus if the edited region contained one character end_pos - start_pos should equal 1. The start and end points are based on the edited region of the file before the edit.)
DOC_PARAM(character_shift_amount, The shift in the characters after the edited region.)
DOC_RETURN(Returns a partially initialized relex state.)
DOC(This call does the first setup step of initializing a relex state. To finish initializing the relex state
you must tell the state about the positioning of the first chunk it will be fed. There are two methods of doing
this, the direct method is with cpp_relex_declare_first_chunk_position, the method that is often more convenient
is with cpp_relex_is_start_chunk. If the file is not chunked the second step of initialization can be skipped.)
DOC(This call does the first setup step of initializing a relex state. To finish initializing the relex state you must tell the state about the positioning of the first chunk it will be fed. There are two methods of doing this, the direct method is with cpp_relex_declare_first_chunk_position, the method that is often more convenient is with cpp_relex_is_start_chunk. If the file is not chunked the second step of initialization can be skipped.)
DOC_SEE(cpp_relex_declare_first_chunk_position)
DOC_SEE(cpp_relex_is_start_chunk)
*/{
Cpp_Relex_Data state = {0};
@ -1536,7 +1524,7 @@ DOC_SEE(cpp_make_token_array)
}
API_EXPORT FCPP_LINK void
cpp_resize_token_array(Cpp_Token_Array *token_array, i32_4tech new_max)/*
cpp_resize_token_array(Cpp_Token_Array *token_array, u32_4tech new_max)/*
DOC_PARAM(token_array, An array previously allocated by cpp_make_token_array.)
DOC_PARAM(new_max, The new maximum size the array should support. If this is not greater
than the current size of the array the operation is ignored.)

View File

@ -1,7 +1,7 @@
// TOP
#ifndef FCPP_LEXER_TYPES_INC
#if !defined(FCPP_LEXER_TYPES_INC)
#define FCPP_LEXER_TYPES_INC
#ifndef ENUM
@ -237,10 +237,10 @@ STRUCT Cpp_Token{
Cpp_Token_Type type;
/* DOC(The start field indicates the index of the first character of this token's lexeme.) */
int32_t start;
uint32_t start;
/* DOC(The size field indicates the number of bytes in this token's lexeme.) */
int32_t size;
uint32_t size;
/* DOC(The state_flags should not be used outside of the lexer's implementation.) */
uint16_t state_flags;
@ -273,10 +273,10 @@ STRUCT Cpp_Token_Array{
Cpp_Token *tokens;
/* DOC(The count field counts how many tokens in the array are currently used.) */
int32_t count;
uint32_t count;
/* DOC(The max_count field specifies the maximum size the count field may grow to before the tokens array is out of space.) */
int32_t max_count;
uint32_t max_count;
};
static Cpp_Token_Array null_cpp_token_array = {0};
@ -288,13 +288,13 @@ STRUCT Cpp_Get_Token_Result{
int32_t token_index;
/* DOC(The in_whitespace field is true when the query position was actually in whitespace after the result token.) */
int32_t in_whitespace;
uint32_t in_whitespace;
/* DOC(If the token_index refers to an actual token, this is the start value of the token. Otherwise this is zero.) */
int32_t token_start;
uint32_t token_start;
/* DOC(If the token_index refers to an actual token, this is the start+size value of the token. Otherwise this is zero.) */
int32_t token_end;
uint32_t token_end;
};
/* DOC(Cpp_Relex_Range is the return result of the cpp_get_relex_range call.)
@ -321,12 +321,12 @@ DOC_SEE(cpp_lex_data_init)
HIDE_MEMBERS() */
STRUCT Cpp_Lex_Data{
char tb[32];
int32_t tb_pos;
int32_t token_start;
uint32_t tb_pos;
uint32_t token_start;
int32_t pos;
int32_t pos_overide;
int32_t chunk_pos;
uint32_t pos;
uint32_t pos_overide;
uint32_t chunk_pos;
Cpp_Lex_FSM fsm;
uint8_t white_done;
@ -361,10 +361,10 @@ STRUCT Cpp_Relex_Data{
Cpp_Token end_token;
int32_t relex_start_position;
int32_t start_token_index;
int32_t end_token_index;
int32_t original_end_token_index;
uint32_t relex_start_position;
uint32_t start_token_index;
uint32_t end_token_index;
uint32_t original_end_token_index;
int32_t character_shift_amount;

View File

@ -585,7 +585,7 @@ DOC_SEE(4coder_Buffer_Positioning_System)
}
API_EXPORT bool32
Buffer_Replace_Range(Application_Links *app, Buffer_Summary *buffer, int32_t start, int32_t end, char *str, int32_t len)
Buffer_Replace_Range(Application_Links *app, Buffer_Summary *buffer, size_t start, size_t end, char *str, size_t len)
/*
DOC_PARAM(buffer, This parameter specifies the buffer to edit.)
DOC_PARAM(start, This parameter specifies absolute position of the first character in the replace range.)
@ -656,7 +656,7 @@ DOC_SEE(Partial_Cursor)
}
API_EXPORT bool32
Buffer_Batch_Edit(Application_Links *app, Buffer_Summary *buffer, char *str, int32_t str_len, Buffer_Edit *edits, int32_t edit_count, Buffer_Batch_Edit_Type type)
Buffer_Batch_Edit(Application_Links *app, Buffer_Summary *buffer, char *str, size_t str_len, Buffer_Edit *edits, uint32_t edit_count, Buffer_Batch_Edit_Type type)
/*
DOC_PARAM(buffer, The buffer on which to apply the batch of edits.)
DOC_PARAM(str, This parameter provides all of the source string for the edits in the batch.)
@ -2287,7 +2287,7 @@ DOC(This is a temporary ad-hoc solution to allow some customization of the behav
}
API_EXPORT void*
Memory_Allocate(Application_Links *app, int32_t size)
Memory_Allocate(Application_Links *app, size_t size)
/*
DOC_PARAM(size, The size in bytes of the block that should be returned.)
DOC(This calls to a low level OS allocator which means it is best used for infrequent, large allocations. The size of the block must be remembered if it will be freed or if it's mem protection status will be changed.)
@ -2300,7 +2300,7 @@ DOC_SEE(memory_free)
}
API_EXPORT bool32
Memory_Set_Protection(Application_Links *app, void *ptr, int32_t size, Memory_Protect_Flags flags)
Memory_Set_Protection(Application_Links *app, void *ptr, size_t size, Memory_Protect_Flags flags)
/*
DOC_PARAM(ptr, The base of the block on which to set memory protection flags.)
DOC_PARAM(size, The size that was originally used to allocate this block.)
@ -2316,7 +2316,7 @@ DOC_SEE(Memory_Protect_Flags)
}
API_EXPORT void
Memory_Free(Application_Links *app, void *ptr, int32_t size)
Memory_Free(Application_Links *app, void *ptr, size_t size)
/*
DOC_PARAM(mem, The base of a block to free.)
DOC_PARAM(size, The size that was originally used to allocate this block.)

View File

@ -188,9 +188,9 @@ buffer_batch_edit_update_cursors(Cursor_With_Index *sorted_positions, i32 count,
//////////////////////////////////////
internal i32
eol_convert_in(char *dest, char *src, i32 size){
i32 i = 0, j = 0, k = 0;
internal umem
eol_convert_in(char *dest, char *src, umem size){
umem i = 0, j = 0, k = 0;
for (; j < size && src[j] != '\r'; ++j);
memcpy(dest, src, j);
@ -212,9 +212,9 @@ eol_convert_in(char *dest, char *src, i32 size){
return(j);
}
internal i32
eol_in_place_convert_in(char *data, i32 size){
i32 i = 0, j = 0, k = 0;
internal umem
eol_in_place_convert_in(char *data, umem size){
umem i = 0, j = 0, k = 0;
for (; j < size && data[j] != '\r'; ++j);
@ -236,10 +236,10 @@ eol_in_place_convert_in(char *data, i32 size){
}
// TODO(allen): iterative memory check?
internal i32
eol_convert_out(char *dest, i32 max, char *src, i32 size, i32 *size_out){
i32 result = 1;
i32 i = 0, j = 0;
internal b32
eol_convert_out(char *dest, umem max, char *src, umem size, umem *size_out){
b32 result = true;
umem i = 0, j = 0;
for (; i < size; ++i, ++j){
if (src[i] == '\n'){
@ -247,7 +247,9 @@ eol_convert_out(char *dest, i32 max, char *src, i32 size, i32 *size_out){
++j;
dest[j] = '\n';
}
else dest[j] = src[i];
else{
dest[j] = src[i];
}
}
*size_out = j;
@ -255,10 +257,10 @@ eol_convert_out(char *dest, i32 max, char *src, i32 size, i32 *size_out){
}
// TODO(allen): iterative memory check?
internal i32
eol_in_place_convert_out(char *data, i32 size, i32 max, i32 *size_out){
i32 result = 1;
i32 i = 0;
internal b32
eol_in_place_convert_out(char *data, umem size, umem max, umem *size_out){
b32 result = true;
umem i = 0;
for (; i < size; ++i){
if (data[i] == '\n'){
@ -281,32 +283,32 @@ eol_in_place_convert_out(char *data, i32 size, i32 max, i32 *size_out){
typedef struct Gap_Buffer{
char *data;
i32 size1;
i32 gap_size;
i32 size2;
i32 max;
umem size1;
umem gap_size;
umem size2;
umem max;
i32 *line_starts;
i32 line_count;
i32 line_max;
umem *line_starts;
umem line_count;
umem line_max;
} Gap_Buffer;
inline i32
inline b32
buffer_good(Gap_Buffer *buffer){
i32 good = (buffer->data != 0);
b32 good = (buffer->data != 0);
return(good);
}
inline i32
inline umem
buffer_size(Gap_Buffer *buffer){
i32 size = buffer->size1 + buffer->size2;
umem size = buffer->size1 + buffer->size2;
return(size);
}
typedef struct Gap_Buffer_Init{
Gap_Buffer *buffer;
char *data;
i32 size;
umem size;
} Gap_Buffer_Init;
internal Gap_Buffer_Init
@ -318,16 +320,18 @@ buffer_begin_init(Gap_Buffer *buffer, char *data, i32 size){
return(init);
}
internal i32
internal b32
buffer_init_need_more(Gap_Buffer_Init *init){
i32 result = 1;
if (init->buffer->data) result = 0;
b32 result = true;
if (init->buffer->data){
result = false;
}
return(result);
}
internal i32
internal umem
buffer_init_page_size(Gap_Buffer_Init *init){
i32 result = init->size * 2;
umem result = init->size * 2;
return(result);
}
@ -339,15 +343,15 @@ buffer_init_provide_page(Gap_Buffer_Init *init, void *page, i32 page_size){
}
internal b32
buffer_end_init(Gap_Buffer_Init *init, void *scratch, i32 scratch_size){
buffer_end_init(Gap_Buffer_Init *init, void *scratch, umem scratch_size){
Gap_Buffer *buffer = init->buffer;
b32 result = false;
if (buffer->data && buffer->max >= init->size){
i32 size = init->size;
i32 size2 = size >> 1;
i32 osize1 = size - size2;
i32 size1 = osize1;
umem size = init->size;
umem size2 = size*2;
umem osize1 = size - size2;
umem size1 = osize1;
if (size1 > 0){
size1 = eol_convert_in(buffer->data, init->data, size1);
@ -370,18 +374,17 @@ buffer_end_init(Gap_Buffer_Init *init, void *scratch, i32 scratch_size){
typedef struct Gap_Buffer_Stream{
Gap_Buffer *buffer;
char *data;
i32 end;
i32 separated;
i32 absolute_end;
umem end;
umem absolute_end;
b32 separated;
b32 use_termination_character;
char terminator;
} Gap_Buffer_Stream;
static Gap_Buffer_Stream null_buffer_stream = {0};
internal b32
buffer_stringify_loop(Gap_Buffer_Stream *stream, Gap_Buffer *buffer, i32 start, i32 end){
b32 result = 0;
buffer_stringify_loop(Gap_Buffer_Stream *stream, Gap_Buffer *buffer, umem start, umem end){
b32 result = false;
if (0 <= start && start < end && end <= buffer->size1 + buffer->size2){
stream->buffer = buffer;
@ -389,15 +392,15 @@ buffer_stringify_loop(Gap_Buffer_Stream *stream, Gap_Buffer *buffer, i32 start,
if (start < buffer->size1){
if (buffer->size1 < end){
stream->separated = 1;
stream->separated = true;
}
else{
stream->separated = 0;
stream->separated = false;
}
stream->data = buffer->data;
}
else{
stream->separated = 0;
stream->separated = false;
stream->data = buffer->data + buffer->gap_size;
}
@ -412,17 +415,17 @@ buffer_stringify_loop(Gap_Buffer_Stream *stream, Gap_Buffer *buffer, i32 start,
stream->end = stream->absolute_end;
}
result = 1;
result = true;
}
if (result == 0){
if (!result){
if (stream->use_termination_character){
stream->buffer = buffer;
stream->absolute_end = end;
stream->use_termination_character = 0;
stream->use_termination_character = false;
stream->data = (&stream->terminator) - buffer->size1 - buffer->size2;
stream->end = stream->absolute_end + 1;
result = 1;
result = true;
}
}
@ -431,91 +434,99 @@ buffer_stringify_loop(Gap_Buffer_Stream *stream, Gap_Buffer *buffer, i32 start,
internal b32
buffer_stringify_next(Gap_Buffer_Stream *stream){
b32 result = 0;
b32 result = false;
Gap_Buffer *buffer = stream->buffer;
if (stream->separated){
stream->data = buffer->data + buffer->gap_size;
stream->end = stream->absolute_end;
stream->separated = 0;
result = 1;
stream->separated = false;
result = true;
}
if (result == 0){
if (!result){
if (stream->use_termination_character){
stream->use_termination_character = 0;
stream->use_termination_character = false;
stream->data = (&stream->terminator) - buffer->size1 - buffer->size2;
stream->end = stream->absolute_end + 1;
result = 1;
result = true;
}
}
return(result);
}
internal i32
buffer_replace_range(Gap_Buffer *buffer, i32 start, i32 end, char *str, i32 len, i32 *shift_amount,
void *scratch, i32 scratch_memory, i32 *request_amount){
internal b32
buffer_replace_range(Gap_Buffer *buffer, umem start, umem end, char *str, umem len, i32 *shift_amount, void *scratch, umem scratch_memory, umem *request_amount){
char *data = buffer->data;
i32 size = buffer_size(buffer);
i32 result = 0;
i32 move_size = 0;
umem size = buffer_size(buffer);
b32 result = false;
assert(0 <= start);
assert(start <= end);
assert(end <= size);
*shift_amount = (len - (end - start));
if (*shift_amount + size <= buffer->max){
if (end < buffer->size1){
move_size = buffer->size1 - end;
memmove(data + buffer->size1 + buffer->gap_size - move_size, data + end, move_size);
buffer->size1 -= move_size;
buffer->size2 += move_size;
umem target_length = end - start;
if (target_length <= max_i32 && len <= max_i32){
if (len >= target_length){
*shift_amount = (i32)(len - target_length);
}
if (start > buffer->size1){
move_size = start - buffer->size1;
memmove(data + buffer->size1, data + buffer->size1 + buffer->gap_size, move_size);
buffer->size1 += move_size;
buffer->size2 -= move_size;
else{
*shift_amount = -(i32)(target_length - len);
}
memcpy(data + start, str, len);
buffer->size2 = size - end;
buffer->size1 = start + len;
buffer->gap_size -= *shift_amount;
assert(buffer->size1 + buffer->size2 == size + *shift_amount);
assert(buffer->size1 + buffer->gap_size + buffer->size2 == buffer->max);
}
else{
*request_amount = l_round_up_i32(2*(*shift_amount + size), 4 << 10);
result = 1;
if (*shift_amount + size <= buffer->max){
umem move_size = 0;
if (end < buffer->size1){
move_size = buffer->size1 - end;
memmove(data + buffer->size1 + buffer->gap_size - move_size, data + end, move_size);
buffer->size1 -= move_size;
buffer->size2 += move_size;
}
if (start > buffer->size1){
move_size = start - buffer->size1;
memmove(data + buffer->size1, data + buffer->size1 + buffer->gap_size, move_size);
buffer->size1 += move_size;
buffer->size2 -= move_size;
}
memcpy(data + start, str, len);
buffer->size2 = size - end;
buffer->size1 = start + len;
buffer->gap_size -= *shift_amount;
assert(buffer->size1 + buffer->size2 == size + *shift_amount);
assert(buffer->size1 + buffer->gap_size + buffer->size2 == buffer->max);
}
else{
*request_amount = l_round_up_umem(2*(*shift_amount + size), KB(4));
result = true;
}
}
return(result);
}
typedef struct Buffer_Batch_State{
i32 i;
umem i;
i32 shift_total;
} Buffer_Batch_State;
// TODO(allen): Now that we are just using Gap_Buffer we could afford to improve
// this for the Gap_Buffer's behavior.
internal i32
buffer_batch_edit_step(Buffer_Batch_State *state, Gap_Buffer *buffer, Buffer_Edit *sorted_edits, char *strings, i32 edit_count, void *scratch, i32 scratch_size, i32 *request_amount){
// TODO(allen): Now that we are just using Gap_Buffer we could afford to improve this for the Gap_Buffer's behavior.
internal b32
buffer_batch_edit_step(Buffer_Batch_State *state, Gap_Buffer *buffer, Buffer_Edit *sorted_edits, char *strings, u32 edit_count, void *scratch, umem scratch_size, umem *request_amount){
Buffer_Edit *edit = 0;
i32 i = state->i;
umem i = state->i;
i32 shift_total = state->shift_total;
i32 shift_amount = 0;
i32 result = 0;
b32 result = false;
edit = sorted_edits + i;
for (; i < edit_count; ++i, ++edit){
result = buffer_replace_range(buffer, edit->start + shift_total, edit->end + shift_total,
strings + edit->str_start, edit->len, &shift_amount,
scratch, scratch_size, request_amount);
if (result) break;
result = buffer_replace_range(buffer, edit->start + shift_total, edit->end + shift_total, strings + edit->str_start, edit->len, &shift_amount, scratch, scratch_size, request_amount);
if (result){
break;
}
shift_total += shift_amount;
}
@ -526,10 +537,10 @@ buffer_batch_edit_step(Buffer_Batch_State *state, Gap_Buffer *buffer, Buffer_Edi
}
internal void*
buffer_edit_provide_memory(Gap_Buffer *buffer, void *new_data, i32 new_max){
buffer_edit_provide_memory(Gap_Buffer *buffer, void *new_data, umem new_max){
void *result = buffer->data;
i32 size = buffer_size(buffer);
i32 new_gap_size = new_max - size;
umem size = buffer_size(buffer);
umem new_gap_size = new_max - size;
assert(new_max >= size);
@ -548,14 +559,14 @@ buffer_edit_provide_memory(Gap_Buffer *buffer, void *new_data, i32 new_max){
//
inline void
buffer_stringify(Gap_Buffer *buffer, i32 start, i32 end, char *out){
buffer_stringify(Gap_Buffer *buffer, umem start, umem end, char *out){
Gap_Buffer_Stream stream = {0};
i32 i = start;
umem i = start;
if (buffer_stringify_loop(&stream, buffer, i, end)){
b32 still_looping = 0;
b32 still_looping = false;
do{
i32 size = stream.end - i;
umem size = stream.end - i;
memcpy(out, stream.data + i, size);
i = stream.end;
out += size;
@ -564,19 +575,19 @@ buffer_stringify(Gap_Buffer *buffer, i32 start, i32 end, char *out){
}
}
internal i32
buffer_convert_out(Gap_Buffer *buffer, char *dest, i32 max){
internal umem
buffer_convert_out(Gap_Buffer *buffer, char *dest, umem max){
Gap_Buffer_Stream stream = {0};
i32 i = 0;
i32 size = buffer_size(buffer);
umem size = buffer_size(buffer);
assert(size + buffer->line_count <= max);
i32 pos = 0;
umem pos = 0;
if (buffer_stringify_loop(&stream, buffer, 0, size)){
b32 still_looping = 0;
umem i = 0;
b32 still_looping = false;
do{
i32 chunk_size = stream.end - i;
i32 out_size = 0;
umem chunk_size = stream.end - i;
umem out_size = 0;
i32 result = eol_convert_out(dest + pos, max - pos, stream.data + i, chunk_size, &out_size);
assert(result);
i = stream.end;
@ -612,22 +623,22 @@ buffer_count_newlines(Gap_Buffer *buffer, i32 start, i32 end){
}
typedef struct Buffer_Measure_Starts{
i32 i;
i32 count;
i32 start;
umem i;
umem count;
umem start;
} Buffer_Measure_Starts;
// TODO(allen): Rewrite this with a duff routine
// Also make it so that the array goes one past the end
// and stores the size in the extra spot.
internal i32
internal b32
buffer_measure_starts(Buffer_Measure_Starts *state, Gap_Buffer *buffer){
Gap_Buffer_Stream stream = {0};
i32 size = buffer_size(buffer);
i32 start = state->start, i = state->i;
i32 *start_ptr = buffer->line_starts + state->count;
i32 *start_end = buffer->line_starts + buffer->line_max;
i32 result = 1;
umem size = buffer_size(buffer);
umem start = state->start, i = state->i;
umem *start_ptr = buffer->line_starts + state->count;
umem *start_end = buffer->line_starts + buffer->line_max;
b32 result = true;
if (buffer_stringify_loop(&stream, buffer, i, size)){
b32 still_looping = 0;
@ -646,14 +657,12 @@ buffer_measure_starts(Buffer_Measure_Starts *state, Gap_Buffer *buffer){
still_looping = buffer_stringify_next(&stream);
}while(still_looping);
}
assert(i == size);
if (start_ptr == start_end){
goto buffer_measure_starts_widths_end;
}
*start_ptr++ = start;
result = 0;
result = false;
buffer_measure_starts_widths_end:;
state->i = i;
@ -668,22 +677,19 @@ buffer_measure_starts(Buffer_Measure_Starts *state, Gap_Buffer *buffer){
}
internal void
buffer_measure_character_starts(System_Functions *system, Render_Font *font, Gap_Buffer *buffer, i32 *character_starts, i32 mode, i32 virtual_white){
buffer_measure_character_starts(System_Functions *system, Render_Font *font, Gap_Buffer *buffer, umem *character_starts, i32 mode, b32 virtual_white){
assert(mode == 0);
Gap_Buffer_Stream stream = {0};
i32 i = 0;
i32 size = buffer_size(buffer);
i32 line_index = 0;
i32 character_index = 0;
b32 skipping_whitespace = 0;
umem line_index = 0;
umem character_index = 0;
character_starts[line_index++] = character_index;
b32 skipping_whitespace = false;
if (virtual_white){
skipping_whitespace = 1;
skipping_whitespace = true;
}
Translation_State tran = {0};
@ -691,8 +697,11 @@ buffer_measure_character_starts(System_Functions *system, Render_Font *font, Gap
stream.use_termination_character = 1;
stream.terminator = '\n';
umem size = buffer_size(buffer);
umem i = 0;
if (buffer_stringify_loop(&stream, buffer, i, size)){
b32 still_looping = 0;
b32 still_looping = false;
do{
for (; i < stream.end; ++i){
u8 ch = (u8)stream.data[i];
@ -706,12 +715,12 @@ buffer_measure_character_starts(System_Functions *system, Render_Font *font, Gap
++character_index;
character_starts[line_index++] = character_index;
if (virtual_white){
skipping_whitespace = 1;
skipping_whitespace = true;
}
}
else if (behavior.do_codepoint_advance || behavior.do_number_advance){
if (ch != ' ' && ch != '\t'){
skipping_whitespace = 0;
skipping_whitespace = false;
}
if (!skipping_whitespace){
@ -738,8 +747,8 @@ struct Buffer_Layout_Stop{
u32 status;
i32 line_index;
i32 wrap_line_index;
i32 pos;
i32 next_line_pos;
umem pos;
umem next_line_pos;
f32 x;
};
@ -753,8 +762,8 @@ struct Buffer_Measure_Wrap_Params{
struct Buffer_Measure_Wrap_State{
Gap_Buffer_Stream stream;
i32 i;
i32 size;
umem i;
umem size;
b32 still_looping;
i32 line_index;
@ -911,20 +920,20 @@ buffer_measure_wrap_y(Buffer_Measure_Wrap_State *S_ptr, Buffer_Measure_Wrap_Para
#undef DrReturn
internal void
buffer_remeasure_starts(Gap_Buffer *buffer, i32 line_start, i32 line_end, i32 line_shift, i32 text_shift){
i32 *starts = buffer->line_starts;
i32 line_count = buffer->line_count;
buffer_remeasure_starts(Gap_Buffer *buffer, umem start_line, umem end_line, i32 line_shift, i32 text_shift){
umem *starts = buffer->line_starts;
umem line_count = buffer->line_count;
assert(0 <= line_start);
assert(line_start <= line_end);
assert(line_end < line_count);
assert(0 <= start_line);
assert(start_line <= end_line);
assert(end_line < line_count);
assert(line_count + line_shift <= buffer->line_max);
++line_end;
++end_line;
// Adjust
if (text_shift != 0){
i32 line_i = line_end;
umem line_i = end_line;
starts += line_i;
for (; line_i < line_count; ++line_i, ++starts){
*starts += text_shift;
@ -933,24 +942,24 @@ buffer_remeasure_starts(Gap_Buffer *buffer, i32 line_start, i32 line_end, i32 li
}
// Shift
i32 new_line_count = line_count;
i32 new_line_end = line_end;
umem new_line_count = line_count;
umem new_end_line = end_line;
if (line_shift != 0){
new_line_count += line_shift;
new_line_end += line_shift;
new_end_line += line_shift;
memmove(starts + line_end + line_shift, starts + line_end,
sizeof(i32)*(line_count - line_end));
memmove(starts + end_line + line_shift, starts + end_line,
sizeof(i32)*(line_count - end_line));
}
// Iteration data (yikes! Need better loop system)
Gap_Buffer_Stream stream = {0};
i32 size = buffer_size(buffer);
i32 char_i = starts[line_start];
i32 line_i = line_start;
umem size = buffer_size(buffer);
umem char_i = starts[start_line];
umem line_i = start_line;
// Line start measurement
i32 start = char_i;
umem start = char_i;
if (buffer_stringify_loop(&stream, buffer, char_i, size)){
b32 still_looping = 0;
@ -963,9 +972,8 @@ buffer_remeasure_starts(Gap_Buffer *buffer, i32 line_start, i32 line_end, i32 li
++line_i;
start = char_i + 1;
// TODO(allen): I would like to know that I am not guessing here,
// so let's try to turn the && into an Assert.
if (line_i >= new_line_end && (line_i >= new_line_count || start == starts[line_i])){
// TODO(allen): I would like to know that I am not guessing here, so let's try to turn the && into an Assert.
if (line_i >= new_end_line && (line_i >= new_line_count || start == starts[line_i])){
goto buffer_remeasure_starts_end;
}
}
@ -985,41 +993,40 @@ buffer_remeasure_starts(Gap_Buffer *buffer, i32 line_start, i32 line_end, i32 li
}
internal void
buffer_remeasure_character_starts(System_Functions *system, Render_Font *font, Gap_Buffer *buffer, i32 line_start, i32 line_end, i32 line_shift, i32 *character_starts, i32 mode, i32 virtual_whitespace){
buffer_remeasure_character_starts(System_Functions *system, Render_Font *font, Gap_Buffer *buffer, umem start_line, umem end_line, i32 line_shift, umem *character_starts, i32 mode, b32 virtual_whitespace){
assert(mode == 0);
i32 new_line_count = buffer->line_count;
umem new_line_count = buffer->line_count;
assert(0 <= line_start);
assert(line_start <= line_end);
assert(line_end < new_line_count - line_shift);
assert(0 <= start_line);
assert(start_line <= end_line);
assert(end_line < new_line_count - line_shift);
++line_end;
++end_line;
// Shift
i32 line_count = new_line_count;
i32 new_line_end = line_end;
umem line_count = new_line_count;
umem new_end_line = end_line;
if (line_shift != 0){
line_count -= line_shift;
new_line_end += line_shift;
new_end_line += line_shift;
memmove(character_starts + line_end + line_shift, character_starts + line_end, sizeof(i32)*(line_count - line_end + 1));
memmove(character_starts + end_line + line_shift, character_starts + end_line, sizeof(i32)*(line_count - end_line + 1));
}
// Iteration data
Gap_Buffer_Stream stream = {0};
i32 size = buffer_size(buffer);
i32 char_i = buffer->line_starts[line_start];
i32 line_i = line_start;
umem size = buffer_size(buffer);
umem char_i = buffer->line_starts[start_line];
umem line_i = start_line;
// Character measurement
i32 last_char_start = character_starts[line_i];
i32 current_char_start = last_char_start;
b32 skipping_whitespace = 0;
umem last_char_start = character_starts[line_i];
umem current_char_start = last_char_start;
b32 skipping_whitespace = false;
if (virtual_whitespace){
skipping_whitespace = 1;
skipping_whitespace = true;
}
// Translation
@ -1046,7 +1053,7 @@ buffer_remeasure_character_starts(System_Functions *system, Render_Font *font, G
skipping_whitespace = 1;
}
if (line_i >= new_line_end){
if (line_i >= new_end_line){
goto buffer_remeasure_character_starts_end;
}
}
@ -1064,14 +1071,20 @@ buffer_remeasure_character_starts(System_Functions *system, Render_Font *font, G
still_looping = buffer_stringify_next(&stream);
}while(still_looping);
}
assert(line_i >= new_line_end);
assert(line_i >= new_end_line);
buffer_remeasure_character_starts_end:;
// Adjust
if (line_i <= new_line_end){
i32 character_shift = current_char_start - character_starts[line_i];
if (line_i <= new_end_line){
umem old_value = character_starts[line_i];
umem new_value = current_char_start;
i32 character_shift = 0;
if (new_value > old_value){
character_shift = (i32)(new_value - old_value);
}
else{
character_shift = -(i32)(old_value - new_value);
}
if (character_shift != 0){
character_starts += line_i;
@ -1083,31 +1096,30 @@ buffer_remeasure_character_starts(System_Functions *system, Render_Font *font, G
}
internal void
buffer_remeasure_wrap_y(Gap_Buffer *buffer, i32 line_start, i32 line_end, i32 line_shift,
f32 *wraps, f32 font_height, f32 *adv, f32 max_width){
i32 new_line_count = buffer->line_count;
buffer_remeasure_wrap_y(Gap_Buffer *buffer, umem start_line, umem end_line, i32 line_shift, f32 *wraps, f32 font_height, f32 *adv, f32 max_width){
umem new_line_count = buffer->line_count;
assert(0 <= line_start);
assert(line_start <= line_end);
assert(line_end < new_line_count - line_shift);
assert(0 <= start_line);
assert(start_line <= end_line);
assert(end_line < new_line_count - line_shift);
++line_end;
++end_line;
// Shift
i32 line_count = new_line_count;
i32 new_line_end = line_end;
umem line_count = new_line_count;
umem new_end_line = end_line;
if (line_shift != 0){
line_count -= line_shift;
new_line_end += line_shift;
new_end_line += line_shift;
memmove(wraps + line_end + line_shift, wraps + line_end, sizeof(i32)*(line_count - line_end + 1));
memmove(wraps + end_line + line_shift, wraps + end_line, sizeof(i32)*(line_count - end_line + 1));
}
// Iteration data (yikes! Need better loop system)
Gap_Buffer_Stream stream = {0};
i32 size = buffer_size(buffer);
i32 char_i = buffer->line_starts[line_start];
i32 line_i = line_start;
umem size = buffer_size(buffer);
umem char_i = buffer->line_starts[start_line];
umem line_i = start_line;
// Line wrap measurement
f32 last_wrap = wraps[line_i];
@ -1115,7 +1127,7 @@ buffer_remeasure_wrap_y(Gap_Buffer *buffer, i32 line_start, i32 line_end, i32 li
f32 x = 0.f;
if (buffer_stringify_loop(&stream, buffer, char_i, size)){
b32 still_looping = 0;
b32 still_looping = false;
do{
for (; char_i < stream.end; ++char_i){
u8 ch = (u8)stream.data[char_i];
@ -1128,7 +1140,7 @@ buffer_remeasure_wrap_y(Gap_Buffer *buffer, i32 line_start, i32 line_end, i32 li
x = 0.f;
// TODO(allen): I would like to know that I am not guessing here.
if (line_i >= new_line_end){
if (line_i >= new_end_line){
goto buffer_remeasure_wraps_end;
}
}
@ -1152,7 +1164,7 @@ buffer_remeasure_wrap_y(Gap_Buffer *buffer, i32 line_start, i32 line_end, i32 li
buffer_remeasure_wraps_end:;
// Adjust
if (line_i <= new_line_end){
if (line_i <= new_end_line){
f32 y_shift = current_wrap - wraps[line_i];
if (y_shift != 0){
@ -1164,15 +1176,10 @@ buffer_remeasure_wrap_y(Gap_Buffer *buffer, i32 line_start, i32 line_end, i32 li
}
}
internal i32
binary_search(i32 *array, i32 value, i32 l_bound, i32 u_bound){
i32 start = l_bound, end = u_bound;
i32 i = 0;
if (value < 0){
value = 0;
}
internal umem
binary_search(umem *array, umem value, umem l_bound, umem u_bound){
value = clamp_bottom(0, value);
umem start = l_bound, end = u_bound, i = 0;
for (;;){
i = (start + end) >> 1;
if (array[i] < value){
@ -1190,54 +1197,48 @@ binary_search(i32 *array, i32 value, i32 l_bound, i32 u_bound){
break;
}
}
return(i);
}
inline i32
buffer_get_line_index_range(Gap_Buffer *buffer, i32 pos, i32 l_bound, i32 u_bound){
inline umem
buffer_get_line_index_range(Gap_Buffer *buffer, umem pos, umem l_bound, umem u_bound){
assert(0 <= l_bound);
assert(l_bound <= u_bound);
assert(u_bound <= buffer->line_count);
assert(buffer->line_starts != 0);
i32 i = binary_search(buffer->line_starts, pos, l_bound, u_bound);
umem i = binary_search(buffer->line_starts, pos, l_bound, u_bound);
return(i);
}
inline i32
buffer_get_line_index(Gap_Buffer *buffer, i32 pos){
i32 result = buffer_get_line_index_range(buffer, pos, 0, buffer->line_count);
inline umem
buffer_get_line_index(Gap_Buffer *buffer, umem pos){
umem result = buffer_get_line_index_range(buffer, pos, 0, buffer->line_count);
return(result);
}
inline i32
buffer_get_line_index_from_character_pos(i32 *character_starts, i32 pos, i32 l_bound, i32 u_bound){
i32 i = binary_search(character_starts, pos, l_bound, u_bound);
inline umem
buffer_get_line_index_from_character_pos(umem *character_starts, umem pos, umem l_bound, umem u_bound){
umem i = binary_search(character_starts, pos, l_bound, u_bound);
return(i);
}
inline i32
buffer_get_line_index_from_wrapped_y(i32 *wrap_line_index, f32 y, i32 line_height, i32 l_bound, i32 u_bound){
i32 wrap_index = floor32(y/line_height);
i32 i = binary_search(wrap_line_index, wrap_index, l_bound, u_bound);
inline umem
buffer_get_line_index_from_wrapped_y(umem *wrap_line_index, f32 y, umem line_height, umem l_bound, umem u_bound){
umem wrap_index = floor32(y/line_height);
umem i = binary_search(wrap_line_index, wrap_index, l_bound, u_bound);
return(i);
}
internal Partial_Cursor
buffer_partial_from_pos(Gap_Buffer *buffer, i32 pos){
buffer_partial_from_pos(Gap_Buffer *buffer, umem pos){
Partial_Cursor result = {0};
int32_t size = buffer_size(buffer);
if (pos > size){
pos = size;
}
if (pos < 0){
pos = 0;
}
umem size = buffer_size(buffer);
pos = clamp_umem(0, pos, size);
i32 line_index = buffer_get_line_index_range(buffer, pos, 0, buffer->line_count);
umem line_index = buffer_get_line_index_range(buffer, pos, 0, buffer->line_count);
result.pos = pos;
result.line = line_index+1;
result.character = pos - buffer->line_starts[line_index] + 1;
@ -1720,7 +1721,7 @@ enum Buffer_Render_Flag{
};
typedef struct Buffer_Render_Item{
i32 index;
umem index;
u32 codepoint;
u32 flags;
f32 x0, y0;
@ -1738,8 +1739,7 @@ typedef struct Render_Item_Write{
} Render_Item_Write;
inline Render_Item_Write
write_render_item(Render_Item_Write write, i32 index, u32 codepoint, u32 flags){
write_render_item(Render_Item_Write write, umem index, u32 codepoint, u32 flags){
f32 ch_width = font_get_glyph_advance(write.system, write.font, codepoint);
if (write.x <= write.x_max && write.x + ch_width >= write.x_min){
@ -1950,7 +1950,7 @@ buffer_render_data(Buffer_Render_State *S_ptr, Buffer_Render_Params params, f32
}
if (!S.skipping_whitespace){
u32 I = S.step.i;
umem I = S.step.i;
switch (n){
case '\r':
{
@ -1978,7 +1978,7 @@ buffer_render_data(Buffer_Render_State *S_ptr, Buffer_Render_Params params, f32
}
else if (S.behavior.do_number_advance){
u8 n = (u8)S.step.value;
u32 I = S.step.i;
umem I = S.step.i;
S.skipping_whitespace = false;
S.ch_width = S.byte_advance;

View File

@ -15,7 +15,7 @@
struct Buffer_Model_Step{
u32 type;
u32 value;
i32 i;
umem i;
u32 byte_length;
};

View File

@ -31,7 +31,7 @@ enum{
};
FILE_TRACK_LINK File_Track_Result
init_track_system(File_Track_System *system, Partition *scratch, void *table_memory, i32 table_memory_size, void *listener_memory, i32 listener_memory_size);
init_track_system(File_Track_System *system, Partition *scratch, void *table_memory, umem table_memory_size, void *listener_memory, umem listener_memory_size);
FILE_TRACK_LINK File_Track_Result
add_listener(File_Track_System *system, Partition *scratch, u8 *filename);
@ -40,13 +40,13 @@ FILE_TRACK_LINK File_Track_Result
remove_listener(File_Track_System *system, Partition *scratch, u8 *filename);
FILE_TRACK_LINK File_Track_Result
move_track_system(File_Track_System *system, Partition *scratch, void *mem, i32 size);
move_track_system(File_Track_System *system, Partition *scratch, void *mem, umem size);
FILE_TRACK_LINK File_Track_Result
expand_track_system_listeners(File_Track_System *system, Partition *scratch, void *mem, i32 size);
expand_track_system_listeners(File_Track_System *system, Partition *scratch, void *mem, umem size);
FILE_TRACK_LINK File_Track_Result
get_change_event(File_Track_System *system, Partition *scratch, u8 *buffer, i32 max);
get_change_event(File_Track_System *system, Partition *scratch, u8 *buffer, umem max);
FILE_TRACK_LINK File_Track_Result
shut_down_track_system(File_Track_System *system, Partition *scratch);

View File

@ -12,6 +12,7 @@
typedef struct{
u32 id[4];
} File_Index;
global_const File_Index null_file_index = {0};
typedef u32 rptr32;
@ -25,7 +26,7 @@ typedef struct {
global_const File_Track_Entry null_file_track_entry = {0};
typedef struct {
i32 size;
umem size;
u32 tracked_count;
u32 max;
rptr32 file_table;
@ -36,14 +37,6 @@ typedef struct DLL_Node {
struct DLL_Node *prev;
} DLL_Node;
internal File_Index
zero_file_index(){
File_Index a = {0};
return(a);
}
internal i32
file_hash_is_zero(File_Index a){
return ((a.id[0] == 0) &&
@ -182,18 +175,18 @@ internal_free_slot(File_Track_Tables *tables, File_Track_Entry *entry){
--tables->tracked_count;
}
internal i32
enough_memory_to_init_table(i32 table_memory_size){
i32 result = (sizeof(File_Track_Tables) + FILE_ENTRY_COST*8 <= table_memory_size);
internal b32
enough_memory_to_init_table(umem table_memory_size){
b32 result = (sizeof(File_Track_Tables) + FILE_ENTRY_COST*8 <= table_memory_size);
return(result);
}
internal void
init_table_memory(File_Track_Tables *tables, i32 table_memory_size){
init_table_memory(File_Track_Tables *tables, umem table_memory_size){
tables->size = table_memory_size;
tables->tracked_count = 0;
i32 max_number_of_entries = (table_memory_size - sizeof(*tables)) / FILE_ENTRY_COST;
u32 max_number_of_entries = (u32)(table_memory_size - sizeof(*tables)) / FILE_ENTRY_COST;
tables->file_table = sizeof(*tables);
tables->max = max_number_of_entries;
@ -201,7 +194,7 @@ init_table_memory(File_Track_Tables *tables, i32 table_memory_size){
internal File_Track_Result
move_table_memory(File_Track_Tables *original_tables,
void *mem, i32 size){
void *mem, umem size){
File_Track_Result result = FileTrack_Good;
if (original_tables->size < size){
@ -212,7 +205,7 @@ move_table_memory(File_Track_Tables *original_tables,
tables->size = size;
i32 likely_entry_size = FILE_ENTRY_COST;
i32 max_number_of_entries = (size - sizeof(*tables)) / likely_entry_size;
u32 max_number_of_entries = (u32)(size - sizeof(*tables)) / likely_entry_size;
tables->file_table = sizeof(*tables);
tables->max = max_number_of_entries;

View File

@ -1000,8 +1000,8 @@ struct Code_Wrap_State{
b32 consume_newline;
Gap_Buffer_Stream stream;
i32 size;
i32 i;
umem size;
umem i;
Render_Font *font;
f32 tab_indent_amount;
@ -1055,8 +1055,8 @@ wrap_state_set_top(Code_Wrap_State *state, f32 line_shift){
}
struct Code_Wrap_Step{
i32 position_start;
i32 position_end;
umem position_start;
umem position_end;
f32 start_x;
f32 final_x;
@ -1067,7 +1067,7 @@ struct Code_Wrap_Step{
internal Code_Wrap_Step
wrap_state_consume_token(System_Functions *system, Render_Font *font, Code_Wrap_State *state, i32 fixed_end_point){
Code_Wrap_Step result = {0};
i32 i = state->i;
umem i = state->i;
result.position_start = i;
@ -1501,7 +1501,9 @@ file_measure_wraps(System_Functions *system, Models *models, Editing_File *file,
potential_marks = push_array(part, Potential_Wrap_Indent_Pair, floor32(width));
max_wrap_indent_mark = partition_remaining(part)/sizeof(Wrap_Indent_Pair);
umem remaining = partition_remaining(part);
umem pair_size = sizeof(Wrap_Indent_Pair);
max_wrap_indent_mark = (i32)(remaining/pair_size);
wrap_indent_marks = push_array(part, Wrap_Indent_Pair, max_wrap_indent_mark);
}
@ -1947,7 +1949,7 @@ file_create_from_string(System_Functions *system, Models *models, Editing_File *
buffer_init_provide_page(&init, data, page_size);
}
i32 scratch_size = partition_remaining(part);
umem scratch_size = partition_remaining(part);
Assert(scratch_size > 0);
b32 init_success = buffer_end_init(&init, part->base + part->pos, scratch_size);
AllowLocal(init_success); Assert(init_success);
@ -2200,10 +2202,10 @@ file_first_lex_serial(Mem_Options *mem, Editing_File *file){
Gap_Buffer *buffer = &file->state.buffer;
i32 text_size = buffer_size(buffer);
i32 mem_size = partition_remaining(part);
umem mem_size = partition_remaining(part);
Cpp_Token_Array tokens;
tokens.max_count = mem_size / sizeof(Cpp_Token);
tokens.max_count = (u32)(mem_size / sizeof(Cpp_Token));
tokens.count = 0;
tokens.tokens = push_array(part, Cpp_Token, tokens.max_count);

View File

@ -452,6 +452,13 @@ clamp(u32 a, u32 n, u32 z){
return (n);
}
inline umem
clamp_umem(umem a, umem n, umem z){
if (n < a) n = a;
else if (n > z) n = z;
return (n);
}
#define clamp_top(a,b) Min(a,b)
#define clamp_bottom(a,b) Max(a,b)

View File

@ -169,8 +169,7 @@ struct Full_Job_Data{
struct Unbounded_Work_Queue{
Full_Job_Data *jobs;
i32 count, max, skip;
u32 count, max, skip;
u32 next_job_id;
};
@ -204,13 +203,13 @@ typedef Sys_Acquire_Lock_Sig(System_Acquire_Lock);
typedef Sys_Release_Lock_Sig(System_Release_Lock);
// needed for custom layer
#define Sys_Memory_Allocate_Sig(name) void* name(i32 size)
#define Sys_Memory_Allocate_Sig(name) void* name(umem size)
typedef Sys_Memory_Allocate_Sig(System_Memory_Allocate);
#define Sys_Memory_Set_Protection_Sig(name) bool32 name(void *ptr, i32 size, u32 flags)
#define Sys_Memory_Set_Protection_Sig(name) bool32 name(void *ptr, umem size, u32 flags)
typedef Sys_Memory_Set_Protection_Sig(System_Memory_Set_Protection);
#define Sys_Memory_Free_Sig(name) void name(void *ptr, i32 size)
#define Sys_Memory_Free_Sig(name) void name(void *ptr, umem size)
typedef Sys_Memory_Free_Sig(System_Memory_Free);
#define Sys_File_Exists_Sig(name) b32 name(char *filename, i32 len)

View File

@ -18,17 +18,6 @@
// Standard implementation of file system stuff based on the file track layer.
//
struct Shared_Vars{
File_Track_System track;
void *track_table;
u32 track_table_size;
u32 track_node_size;
Partition scratch;
};
global Shared_Vars shared_vars;
internal void
init_shared_vars(){
umem scratch_size = KB(128);

View File

@ -9,10 +9,7 @@
// TOP
// NOTE(allen): This serves as a list of functions to implement
// in addition to those in 4ed_system.h These are not exposed to
// the application code, but system_shared.cpp
// rely on the functions listed here.
// TODO(allen): Find a new name/classification for this.
#if !defined(FRED_SYSTEM_SHARED_H)
#define FRED_SYSTEM_SHARED_H
@ -24,17 +21,24 @@ struct File_Data{
};
global File_Data null_file_data = {0};
#define Sys_Get_Memory_Sig(name) void* name(i32 size, i32 line_number, char *file_name)
#define Sys_Free_Memory_Sig(name) void name(void *block)
#define Sys_File_Can_Be_Made_Sig(name) b32 name(char *filename)
#define Sys_Get_Binary_Path_Sig(name) i32 name(String *out)
internal Sys_Get_Memory_Sig(system_get_memory_);
internal Sys_Free_Memory_Sig(system_free_memory);
internal Sys_File_Can_Be_Made_Sig(system_file_can_be_made);
#define Sys_Get_Binary_Path_Sig(name) i32 name(String *out)
internal Sys_Get_Binary_Path_Sig(system_get_binary_path);
#define system_get_memory(size) system_get_memory_((size), __LINE__, __FILE__)
/////////////////////////////////////
struct Shared_Vars{
File_Track_System track;
void *track_table;
u32 track_table_size;
u32 track_node_size;
Partition scratch;
};
global Shared_Vars shared_vars;
#endif

View File

@ -13,7 +13,7 @@
struct Translation_State{
u8 fill_buffer[4];
u32 fill_start_i;
umem fill_start_i;
u8 fill_i;
u8 fill_expected;
};
@ -47,7 +47,7 @@ struct Translation_Emits{
#define SINGLE_BYTE_ERROR_CLASS max_u8
internal void
translating_consume_byte(Translation_State *tran, u8 ch, u32 i, u32 size, Translation_Byte_Description *desc_out){
translating_consume_byte(Translation_State *tran, u8 ch, umem i, umem size, Translation_Byte_Description *desc_out){
desc_out->byte_class = 0;
if ((ch >= ' ' && ch < 0x7F) || ch == '\t' || ch == '\n' || ch == '\r'){
desc_out->byte_class = 1;
@ -146,7 +146,7 @@ translating_select_emit_rule_with_font(System_Functions *system, Render_Font *fo
}
internal void
translating_generate_emits(Translation_State *tran, Translation_Emit_Rule emit_rule, u8 ch, u32 i, Translation_Emits *emits_out){
translating_generate_emits(Translation_State *tran, Translation_Emit_Rule emit_rule, u8 ch, umem i, Translation_Emits *emits_out){
emits_out->step_count = 0;
switch (emit_rule.emit_type){
default: goto skip_all;
@ -199,7 +199,7 @@ translating_generate_emits(Translation_State *tran, Translation_Emit_Rule emit_r
}
internal void
translating_fully_process_byte(System_Functions *system, Render_Font *font, Translation_State *tran, u8 ch, u32 i, u32 size, Translation_Emits *emits_out){
translating_fully_process_byte(System_Functions *system, Render_Font *font, Translation_State *tran, u8 ch, umem i, umem size, Translation_Emits *emits_out){
Translation_Byte_Description description = {0};
translating_consume_byte(tran, ch, i, size, &description);
Translation_Emit_Rule emit_rule = {0};

View File

@ -87,6 +87,10 @@ inline u32 l_round_up_u32(u32 x, u32 b){
return( ((x)+(b)-1) - (((x)+(b)-1)%(b)) );
}
inline umem l_round_up_umem(umem x, umem b){
return( ((x)+(b)-1) - (((x)+(b)-1)%(b)) );
}
inline u32 round_up_pot_u32(u32 x){
--x;
x |= x >> 1;

View File

@ -886,7 +886,7 @@ static Argument_Breakdown
parameter_parse(Partition *part, char *data, Cpp_Token *args_start_token, Cpp_Token *args_end_token){
int32_t arg_index = 0;
Cpp_Token *arg_token = args_start_token + 1;
int32_t param_string_start = arg_token->start;
uint32_t param_string_start = arg_token->start;
int32_t arg_count = 1;
arg_token = args_start_token;

View File

@ -81,7 +81,7 @@ do_file_copy(Partition *part, char *src_file, char *dst_dir, char *dst_file){
terminate_with_null(&str);
Temp_Memory temp = begin_temp_memory(part);
int32_t mem_size = partition_remaining(part);
size_t mem_size = partition_remaining(part);
void *mem = push_block(part, mem_size);
FILE *in = fopen(src_file, "rb");
if (in){
@ -89,17 +89,17 @@ do_file_copy(Partition *part, char *src_file, char *dst_dir, char *dst_file){
int32_t file_size = ftell(in);
if (mem_size >= file_size){
fseek(in, 0, SEEK_SET);
fread(mem, 1, file_size, in);
FILE *out = fopen(dst, "wb");
if (out){
fwrite(mem, 1, file_size, out);
fclose(out);
success = 1;
fseek(in, 0, SEEK_SET);
fread(mem, 1, file_size, in);
FILE *out = fopen(dst, "wb");
if (out){
fwrite(mem, 1, file_size, out);
fclose(out);
success = 1;
}
}
}
fclose(in);
}
end_temp_memory(temp);

View File

@ -1116,8 +1116,10 @@ CUSTOM_COMMAND_SIG(rename_parameter){
String replace_string = with.string;
Buffer_Edit *edits = (Buffer_Edit*)partition_current(part);
int32_t edit_max = (partition_remaining(part))/sizeof(Buffer_Edit);
int32_t edit_count = 0;
size_t remaining = partition_remaining(part);
size_t edit_size = sizeof(Buffer_Edit);
uint32_t edit_max = (uint32_t)(remaining/edit_size);
uint32_t edit_count = 0;
if (edit_max >= 1){
Buffer_Edit edit;
@ -1140,7 +1142,7 @@ CUSTOM_COMMAND_SIG(rename_parameter){
switch (token_ptr->type){
case CPP_TOKEN_IDENTIFIER:
{
if (token_ptr->size == old_lexeme.size){
if (token_ptr->size == (uint32_t)old_lexeme.size){
char other_lexeme_base[128];
String other_lexeme = make_fixed_width_string(other_lexeme_base);
other_lexeme.size = old_lexeme.size;
@ -1253,7 +1255,8 @@ CUSTOM_COMMAND_SIG(write_explicit_enum_values){
Buffer_Edit *edits = push_array(part, Buffer_Edit, count_estimate);
char *string_base = (char*)partition_current(part);
String string = make_string(string_base, 0, partition_remaining(part));
uint32_t remaining = (uint32_t)partition_remaining(part);
String string = make_string(string_base, 0, remaining);
closed_correctly = false;
still_looping = false;

View File

@ -283,40 +283,13 @@ Sys_Memory_Free_Sig(system_memory_free){
VirtualFree(ptr, 0, MEM_RELEASE);
}
// TODO(allen): delete
internal
Sys_Get_Memory_Sig(system_get_memory_){
void *ptr = 0;
if (size > 0){
ptr = VirtualAlloc(0, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
}
return(ptr);
}
// TODO(allen): delete
internal
Sys_Free_Memory_Sig(system_free_memory){
if (block){
VirtualFree(block, 0, MEM_RELEASE);
}
}
#define Win32GetMemory(size) system_get_memory_(size, __LINE__, __FILE__)
#define Win32FreeMemory(ptr) system_free_memory(ptr)
#define Win32GetMemory(size) system_memory_allocate(size)
#define Win32FreeMemory(ptr) system_memory_free(ptr)
#define Win32ScratchPartition sysshared_scratch_partition
#define Win32ScratchPartitionGrow sysshared_partition_grow
#define Win32ScratchPartitionDouble sysshared_partition_double
#if 0
#if FRED_INTERNAL
internal void
INTERNAL_system_debug_message(char *message){
OutputDebugStringW(message);
}
#endif
#endif
//
// Multithreading
@ -407,8 +380,8 @@ JobThreadProc(LPVOID lpParameter){
internal void
initialize_unbounded_queue(Unbounded_Work_Queue *source_queue){
i32 max = 512;
source_queue->jobs = (Full_Job_Data*)system_get_memory(max*sizeof(Full_Job_Data));
u32 max = 512;
source_queue->jobs = (Full_Job_Data*)system_memory_allocate(max*sizeof(Full_Job_Data));
source_queue->count = 0;
source_queue->max = max;
source_queue->skip = 0;
@ -520,11 +493,11 @@ Sys_Post_Job_Sig(system_post_job){
while (queue->count >= queue->max){
i32 new_max = queue->max*2;
Full_Job_Data *new_jobs = (Full_Job_Data*)
system_get_memory(new_max*sizeof(Full_Job_Data));
system_memory_allocate(new_max*sizeof(Full_Job_Data));
memcpy(new_jobs, queue->jobs, queue->count);
system_free_memory(queue->jobs);
system_memory_free(queue->jobs, 0);
queue->jobs = new_jobs;
queue->max = new_max;
@ -614,11 +587,11 @@ Sys_Grow_Thread_Memory_Sig(system_grow_thread_memory){
void *old_data = memory->data;
i32 old_size = memory->size;
i32 new_size = l_round_up_i32(memory->size*2, KB(4));
memory->data = system_get_memory(new_size);
memory->data = system_memory_allocate(new_size);
memory->size = new_size;
if (old_data){
memcpy(memory->data, old_data, old_size);
system_free_memory(old_data);
system_memory_free(old_data, 0);
}
system_release_lock(CANCEL_LOCK0 + memory->id - 1);
}
@ -744,15 +717,29 @@ Sys_Yield_Coroutine_Sig(system_yield_coroutine){
internal
Sys_File_Can_Be_Made_Sig(system_file_can_be_made){
HANDLE file = CreateFile((char*)filename, FILE_APPEND_DATA, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
b32 result = false;
if (!file || file == INVALID_HANDLE_VALUE){
return 0;
Partition *scratch = &shared_vars.scratch;
Temp_Memory temp = begin_temp_memory(scratch);
umem len = str_size(filename);
umem max = (len+1)*2;
u16 *filename_16 = push_array(scratch, u16, max);
b32 error = false;
utf8_to_utf16_minimal_checking(filename_16, max, filename, len, &error);
if (!error){
HANDLE file = CreateFile((LPCWSTR)filename_16, FILE_APPEND_DATA, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (file != 0 && file != INVALID_HANDLE_VALUE){
result = true;
CloseHandle(file);
}
}
CloseHandle(file);
end_temp_memory(temp);
return(1);
return(result);
}
internal
@ -1386,8 +1373,6 @@ Sys_Send_Exit_Signal_Sig(system_send_exit_signal){
#include "4ed_system_shared.cpp"
#include "win32_4ed_fonts.cpp"
//
// Linkage to Custom and Application
//
@ -2469,6 +2454,9 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdS
#include "font/4coder_font_static_functions.cpp"
#include "win32_4ed_fonts.cpp"
//#include "win32_4ed_file_track.cpp"
#if 0
// NOTE(allen): In case I want to switch back to a console
// application at some point.

View File

@ -44,7 +44,7 @@ typedef struct {
#define to_tables(v) ((File_Track_Tables*)(v->tables))
FILE_TRACK_LINK File_Track_Result
init_track_system(File_Track_System *system, Partition *scratch, void *table_memory, i32 table_memory_size, void *listener_memory, i32 listener_memory_size){
init_track_system(File_Track_System *system, Partition *scratch, void *table_memory, umem table_memory_size, void *listener_memory, umem listener_memory_size){
File_Track_Result result = FileTrack_MemoryTooSmall;
Win32_File_Track_Vars *vars = to_vars(system);
@ -63,8 +63,9 @@ init_track_system(File_Track_System *system, Partition *scratch, void *table_mem
init_sentinel_node(&vars->free_sentinel);
Win32_Directory_Listener_Node *listener = (Win32_Directory_Listener_Node*)listener_memory;
i32 count = listener_memory_size / sizeof(Win32_Directory_Listener_Node);
for (i32 i = 0; i < count; ++i, ++listener){
umem node_size = sizeof(Win32_Directory_Listener_Node);
u32 count = (u32)(listener_memory_size / node_size);
for (u32 i = 0; i < count; ++i, ++listener){
insert_node(&vars->free_sentinel, &listener->node);
}
}
@ -254,7 +255,7 @@ remove_listener(File_Track_System *system, Partition *scratch, u8 *filename){
}
FILE_TRACK_LINK File_Track_Result
move_track_system(File_Track_System *system, Partition *scratch, void *mem, i32 size){
move_track_system(File_Track_System *system, Partition *scratch, void *mem, umem size){
File_Track_Result result = FileTrack_Good;
Win32_File_Track_Vars *vars = to_vars(system);
@ -270,7 +271,7 @@ move_track_system(File_Track_System *system, Partition *scratch, void *mem, i32
}
FILE_TRACK_LINK File_Track_Result
expand_track_system_listeners(File_Track_System *system, Partition *scratch, void *mem, i32 size){
expand_track_system_listeners(File_Track_System *system, Partition *scratch, void *mem, umem size){
File_Track_Result result = FileTrack_Good;
Win32_File_Track_Vars *vars = to_vars(system);
@ -278,8 +279,9 @@ expand_track_system_listeners(File_Track_System *system, Partition *scratch, voi
if (sizeof(Win32_Directory_Listener_Node) <= size){
Win32_Directory_Listener_Node *listener = (Win32_Directory_Listener_Node*)mem;
i32 count = size / sizeof(Win32_Directory_Listener_Node);
for (i32 i = 0; i < count; ++i, ++listener){
umem node_size = sizeof(Win32_Directory_Listener_Node);
u32 count = (u32)(size / node_size);
for (u32 i = 0; i < count; ++i, ++listener){
insert_node(&vars->free_sentinel, &listener->node);
}
}
@ -293,7 +295,7 @@ expand_track_system_listeners(File_Track_System *system, Partition *scratch, voi
}
FILE_TRACK_LINK File_Track_Result
get_change_event(File_Track_System *system, Partition *scratch, u8 *buffer, i32 max, i32 *size){
get_change_event(File_Track_System *system, Partition *scratch, u8 *buffer, i32 max, umem *size){
File_Track_Result result = FileTrack_NoMoreEvents;
Win32_File_Track_Vars *vars = to_vars(system);
@ -301,17 +303,19 @@ get_change_event(File_Track_System *system, Partition *scratch, u8 *buffer, i32
local_persist DWORD offset = 0;
local_persist Win32_Directory_Listener listener = {0};
Temp_Memory temp = begin_temp_memory(scratch);
EnterCriticalSection(&vars->table_lock);
OVERLAPPED *overlapped = 0;
DWORD length = 0;
ULONG_PTR key = 0;
b32 has_result = 0;
b32 has_result = false;
if (has_buffered_event){
has_buffered_event = 0;
has_result = 1;
has_result = true;
}
else{
if (GetQueuedCompletionStatus(vars->iocp, &length, &key, &overlapped, 0)){
@ -324,7 +328,7 @@ get_change_event(File_Track_System *system, Partition *scratch, u8 *buffer, i32
ReadDirectoryChangesW(listener_ptr->dir, listener_ptr->result, sizeof(listener_ptr->result), 1, FLAGS, 0, &listener_ptr->overlapped, 0);
offset = 0;
has_result = 1;
has_result = true;
}
}
@ -335,36 +339,39 @@ get_change_event(File_Track_System *system, Partition *scratch, u8 *buffer, i32
i32 dir_len = GetFinalPathNameByHandle(listener.dir, 0, 0, FILE_NAME_NORMALIZED);
i32 req_size = dir_len + 1 + len;
*size = req_size;
if (req_size < max){
i32 pos = GetFinalPathNameByHandle(listener.dir, buffer, max, FILE_NAME_NORMALIZED);
buffer[pos++] = '\\';
u16 *buffer_16 = push_array(scratch, u16, req_size+1);
i32 pos = GetFinalPathNameByHandle(listener.dir, (LPWSTR)buffer_16, max, FILE_NAME_NORMALIZED);
for (i32 i = 0; i < len; ++i, ++pos){
buffer[pos] = (char)info->FileName[i];
b32 convert_error = false;
umem buffer_size = utf16_to_utf8_minimal_checking(buffer, max-1, buffer_16, pos, &convert_error);
if (buffer_size > max-1){
result = FileTrack_MemoryTooSmall;
}
if (buffer[0] == '\\'){
for (i32 i = 0; i+4 < pos; ++i){
buffer[i] = buffer[i+4];
else if (!convert_error){
buffer[buffer_size++] = '\\';
if (buffer[0] == '\\'){
for (i32 i = 0; i+4 < buffer_size; ++i){
buffer[i] = buffer[i+4];
}
buffer_size -= 4;
}
*size -= 4;
*size = buffer_size;
result = FileTrack_Good;
}
else{
result = FileTrack_FileSystemError;
}
result = FileTrack_Good;
}
else{
// TODO(allen): Need some way to stash this result so that if the
// user comes back with more memory we can give them the change
// notification they missed.
// TODO(allen): Need some way to stash this result so that if the user comes back with more memory we can give them the change notification they missed.
result = FileTrack_MemoryTooSmall;
}
if (info->NextEntryOffset != 0){
// TODO(allen): We're not ready to handle this yet.
// For now I am breaking. In the future, if there
// are more results we should stash them and return
// them in future calls.
// TODO(allen): We're not ready to handle this yet. For now I am breaking. In the future, if there are more results we should stash them and return them in future calls.
offset += info->NextEntryOffset;
has_buffered_event = 1;
}
@ -372,6 +379,8 @@ get_change_event(File_Track_System *system, Partition *scratch, u8 *buffer, i32
LeaveCriticalSection(&vars->table_lock);
end_temp_memory(temp);
return(result);
}