From: Mathieu Desnoyers Date: Fri, 6 May 2011 23:04:22 +0000 (-0400) Subject: init pos cleanup X-Git-Tag: v0.1~100 X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=8563e754804a60faf870282d494ea419dc87016b init pos cleanup Signed-off-by: Mathieu Desnoyers --- diff --git a/formats/ctf/ctf.c b/formats/ctf/ctf.c index 8556055e..d214d1aa 100644 --- a/formats/ctf/ctf.c +++ b/formats/ctf/ctf.c @@ -76,7 +76,7 @@ struct format ctf_format = { .close_trace = ctf_close_trace, }; -void ctf_init_pos(struct ctf_stream_pos *pos, int fd) +void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags) { pos->fd = fd; pos->mmap_offset = 0; @@ -86,29 +86,29 @@ void ctf_init_pos(struct ctf_stream_pos *pos, int fd) pos->base = NULL; pos->offset = 0; pos->dummy = false; - pos->packet_index = g_array_new(FALSE, TRUE, - sizeof(struct packet_index)); pos->cur_index = 0; - if (fd >= 0) { - int flags = fcntl(fd, F_GETFL, 0); - - switch (flags & O_ACCMODE) { - case O_RDONLY: - pos->prot = PROT_READ; - pos->flags = MAP_PRIVATE; - pos->parent.rw_table = read_dispatch_table; - break; - case O_WRONLY: - case O_RDWR: - pos->prot = PROT_WRITE; /* Write has priority */ - pos->flags = MAP_SHARED; - pos->parent.rw_table = write_dispatch_table; - ctf_move_pos_slow(pos, 0); /* position for write */ - break; - default: - assert(0); - } + if (fd >= 0) + pos->packet_index = g_array_new(FALSE, TRUE, + sizeof(struct packet_index)); + else + pos->packet_index = NULL; + switch (open_flags & O_ACCMODE) { + case O_RDONLY: + pos->prot = PROT_READ; + pos->flags = MAP_PRIVATE; + pos->parent.rw_table = read_dispatch_table; + break; + case O_WRONLY: + case O_RDWR: + pos->prot = PROT_WRITE; /* Write has priority */ + pos->flags = MAP_SHARED; + pos->parent.rw_table = write_dispatch_table; + if (fd >= 0) + ctf_move_pos_slow(pos, 0); /* position for write */ + break; + default: + assert(0); } } @@ -453,7 +453,7 @@ int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags) if (ret < 0) goto error; file_stream = g_new0(struct ctf_file_stream, 1); - ctf_init_pos(&file_stream->pos, ret); + ctf_init_pos(&file_stream->pos, ret, flags); ret = create_stream_packet_index(td, file_stream); if (ret) goto error_index; diff --git a/formats/ctf/types/float.c b/formats/ctf/types/float.c index a30c3ea4..8bb3f743 100644 --- a/formats/ctf/types/float.c +++ b/formats/ctf/types/float.c @@ -120,7 +120,7 @@ void ctf_float_read(struct stream_pos *ppos, struct definition *definition) container_of(tmpdef, struct definition_float, p); struct ctf_stream_pos destp; - ctf_init_pos(&destp, -1); + ctf_init_pos(&destp, -1, O_WRONLY); destp.base = (char *) u.bits; ctf_align_pos(pos, float_declaration->p.alignment); @@ -144,7 +144,7 @@ void ctf_float_write(struct stream_pos *ppos, struct definition *definition) container_of(tmpdef, struct definition_float, p); struct ctf_stream_pos srcp; - ctf_init_pos(&srcp, -1); + ctf_init_pos(&srcp, -1, O_RDONLY); srcp.base = (char *) u.bits; u.v = float_definition->value; diff --git a/formats/ctf/types/string.c b/formats/ctf/types/string.c index b19335e2..b648b817 100644 --- a/formats/ctf/types/string.c +++ b/formats/ctf/types/string.c @@ -20,27 +20,6 @@ #include /* C99 limits */ #include -void ctf_string_copy(struct stream_pos *pdest, struct stream_pos *psrc, - const struct declaration_string *string_declaration) -{ - struct ctf_stream_pos *dest = ctf_pos(pdest); - struct ctf_stream_pos *src = ctf_pos(psrc); - size_t len; - char *destaddr, *srcaddr; - - ctf_align_pos(src, string_declaration->p.alignment); - srcaddr = ctf_get_pos_addr(src); - len = strlen(srcaddr) + 1; - if (dest->dummy) - goto end; - ctf_align_pos(dest, string_declaration->p.alignment); - destaddr = ctf_get_pos_addr(dest); - strcpy(destaddr, srcaddr); -end: - ctf_move_pos(dest, len); - ctf_move_pos(src, len); -} - void ctf_string_read(struct stream_pos *ppos, struct definition *definition) { struct definition_string *string_definition = @@ -81,7 +60,7 @@ void ctf_string_write(struct stream_pos *ppos, if (pos->dummy) goto end; destaddr = ctf_get_pos_addr(pos); - strcpy(destaddr, string_definition->value); + memcpy(destaddr, string_definition->value, len); end: ctf_move_pos(pos, len); } diff --git a/include/babeltrace/ctf/types.h b/include/babeltrace/ctf/types.h index 121beda5..9a1a4d41 100644 --- a/include/babeltrace/ctf/types.h +++ b/include/babeltrace/ctf/types.h @@ -20,6 +20,9 @@ */ #include +#include +#include +#include #include #include #include @@ -84,7 +87,7 @@ void ctf_sequence_rw(struct stream_pos *pos, struct definition *definition); void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset); -void ctf_init_pos(struct ctf_stream_pos *pos, int fd); +void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags); void ctf_fini_pos(struct ctf_stream_pos *pos); /* diff --git a/tests/test-dummytrace.c b/tests/test-dummytrace.c index ad0ff204..ca79115f 100644 --- a/tests/test-dummytrace.c +++ b/tests/test-dummytrace.c @@ -126,7 +126,7 @@ void trace_text(FILE *input, int output) char *line = NULL, *nl; size_t linesize; - ctf_init_pos(&pos, output); + ctf_init_pos(&pos, output, O_WRONLY); write_packet_header(&pos, s_uuid); write_packet_context(&pos);