Navigation Menu

Skip to content

Commit

Permalink
windows: use memcpy_s() on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Apr 15, 2015
1 parent 5d2c354 commit e277e1e
Show file tree
Hide file tree
Showing 28 changed files with 218 additions and 162 deletions.
1 change: 1 addition & 0 deletions include/groonga.h
Expand Up @@ -18,6 +18,7 @@
#ifndef GROONGA_H
#define GROONGA_H

#include "groonga/compat.h"
#include "groonga/groonga.h"
#include "groonga/obj.h"
#include "groonga/ii.h"
Expand Down
1 change: 1 addition & 0 deletions include/groonga/Makefile.am
@@ -1,6 +1,7 @@
groonga_includedir = $(pkgincludedir)/groonga
groonga_include_HEADERS = \
command.h \
compat.h \
expr.h \
groonga.h \
ii.h \
Expand Down
35 changes: 35 additions & 0 deletions include/groonga/compat.h
@@ -0,0 +1,35 @@
/*
Copyright(C) 2015 Brazil
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef GROONGA_COMPAT_H
#define GROONGA_COMPAT_H

#ifdef WIN32
# ifdef __cplusplus
# define grn_memcpy(dest, src, n) ::memcpy_s((dest), (n), (src), (n))
# else /* __cplusplus */
# define grn_memcpy(dest, src, n) memcpy_s((dest), (n), (src), (n))
# endif /* __cplusplus */
#else /* WIN32 */
# ifdef __cplusplus
# define grn_memcpy(dest, src, n) std::memcpy((dest), (src), (n))
# else /* __cplusplus */
# define grn_memcpy(dest, src, n) memcpy((dest), (src), (n))
# endif /* __cplusplus */
#endif /* WIN32 */

#endif /* GROONGA_COMPAT_H */
4 changes: 2 additions & 2 deletions lib/com.c
Expand Up @@ -528,7 +528,7 @@ grn_com_receiver(grn_ctx *ctx, grn_com *com)
grn_msg *msg = (grn_msg *)grn_msg_open(ctx, com, &ev->recv_old);
grn_com_recv(ctx, msg->u.peer, &msg->header, (grn_obj *)msg);
if (msg->u.peer /* is_edge_request(msg)*/) {
memcpy(&msg->edge_id, &ev->curr_edge_id, sizeof(grn_com_addr));
grn_memcpy(&msg->edge_id, &ev->curr_edge_id, sizeof(grn_com_addr));
if (!com->has_sid) {
com->has_sid = 1;
com->sid = ev->curr_edge_id.sid++;
Expand Down Expand Up @@ -1069,7 +1069,7 @@ grn_com_sopen(grn_ctx *ctx, grn_com_event *ev,
SOERR("socket");
goto exit;
}
memcpy(&ev->curr_edge_id.addr, he->h_addr, he->h_length);
grn_memcpy(&ev->curr_edge_id.addr, he->h_addr, he->h_length);
ev->curr_edge_id.port = htons(port);
ev->curr_edge_id.sid = 0;
{
Expand Down
6 changes: 3 additions & 3 deletions lib/ctx.c
Expand Up @@ -253,7 +253,7 @@ grn_alloc_info_set_backtrace(char *buffer, size_t size)
if (symbol_length + 2 > rest) {
break;
}
memcpy(buffer, symbols[i], symbol_length);
grn_memcpy(buffer, symbols[i], symbol_length);
buffer += symbol_length;
rest -= symbol_length;
buffer[0] = '\n';
Expand Down Expand Up @@ -1997,7 +1997,7 @@ grn_ctx_realloc(grn_ctx *ctx, void *ptr, size_t size,
if (res && ptr) {
int32_t *header = &((int32_t *)ptr)[-2];
size_t size_ = header[1];
memcpy(res, ptr, size_ > size ? size : size_);
grn_memcpy(res, ptr, size_ > size ? size : size_);
grn_ctx_free(ctx, ptr, file, line, func);
}
} else {
Expand All @@ -2013,7 +2013,7 @@ grn_ctx_strdup(grn_ctx *ctx, const char *s, const char* file, int line, const ch
if (s) {
size_t size = strlen(s) + 1;
if ((res = grn_ctx_alloc(ctx, size, 0, file, line, func))) {
memcpy(res, s, size);
grn_memcpy(res, s, size);
}
}
return res;
Expand Down
4 changes: 2 additions & 2 deletions lib/dat.cpp
Expand Up @@ -144,7 +144,7 @@ grn_dat_generate_trie_path(const char *base_path, char *trie_path, uint32_t file
return;
}
const size_t len = std::strlen(base_path);
std::memcpy(trie_path, base_path, len);
grn_memcpy(trie_path, base_path, len);
trie_path[len] = '.';
grn_itoh(file_id % (1U << (4 * FILE_ID_LENGTH)),
trie_path + len + 1, FILE_ID_LENGTH);
Expand Down Expand Up @@ -502,7 +502,7 @@ grn_dat_get_key(grn_ctx *ctx, grn_dat *dat, grn_id id, void *keybuf, int bufsize
return 0;
}
if (keybuf && (bufsize >= (int)key.length())) {
std::memcpy(keybuf, key.ptr(), key.length());
grn_memcpy(keybuf, key.ptr(), key.length());
}
return (int)key.length();
}
Expand Down
6 changes: 6 additions & 0 deletions lib/dat/dat.hpp
Expand Up @@ -42,6 +42,12 @@
# endif // WIN32
#endif // GRN_DAT_API

#ifdef WIN32
# define grn_memcpy(dest, src, n) ::memcpy_s((dest), (n), (src), (n))
#else // WIN32
# define grn_memcpy(dest, src, n) std::memcpy((dest), (src), (n))
#endif // WIN32

namespace grn {
namespace dat {

Expand Down
4 changes: 2 additions & 2 deletions lib/dat/key-cursor.cpp
Expand Up @@ -133,7 +133,7 @@ void KeyCursor::ascending_init(const String &min_str, const String &max_str) {
if (max_str.ptr() != NULL) {
if (max_str.length() != 0) {
end_buf_ = new UInt8[max_str.length()];
std::memcpy(end_buf_, max_str.ptr(), max_str.length());
grn_memcpy(end_buf_, max_str.ptr(), max_str.length());
end_str_.assign(end_buf_, max_str.length());
}
}
Expand Down Expand Up @@ -206,7 +206,7 @@ void KeyCursor::descending_init(const String &min_str, const String &max_str) {
if (min_str.ptr() != NULL) {
if (min_str.length() != 0) {
end_buf_ = new UInt8[min_str.length()];
std::memcpy(end_buf_, min_str.ptr(), min_str.length());
grn_memcpy(end_buf_, min_str.ptr(), min_str.length());
end_str_.assign(end_buf_, min_str.length());
}
}
Expand Down
46 changes: 23 additions & 23 deletions lib/db.c
Expand Up @@ -89,7 +89,7 @@ inline static void
gen_pathname(const char *path, char *buffer, int fno)
{
size_t len = strlen(path);
memcpy(buffer, path, len);
grn_memcpy(buffer, path, len);
if (fno >= 0) {
buffer[len] = '.';
grn_itoh(fno, buffer + len + 1, 7);
Expand Down Expand Up @@ -2063,7 +2063,7 @@ subrecs_push(byte *subrecs, int size, int n_subrecs, double score, void *body, i
}
v = subrecs + n * (GRN_RSET_SCORE_SIZE + size);
*((double *)v) = score;
memcpy(v + GRN_RSET_SCORE_SIZE, body, size);
grn_memcpy(v + GRN_RSET_SCORE_SIZE, body, size);
}

inline static void
Expand Down Expand Up @@ -2097,8 +2097,8 @@ subrecs_replace_min(byte *subrecs, int size, int n_subrecs, double score, void *
}
}
v = subrecs + n * (GRN_RSET_SCORE_SIZE + size);
memcpy(v, &score, GRN_RSET_SCORE_SIZE);
memcpy(v + GRN_RSET_SCORE_SIZE, body, size);
grn_memcpy(v, &score, GRN_RSET_SCORE_SIZE);
grn_memcpy(v + GRN_RSET_SCORE_SIZE, body, size);
}

inline static void
Expand Down Expand Up @@ -3788,7 +3788,7 @@ grn_table_setoperation(grn_ctx *ctx, grn_obj *table1, grn_obj *table2, grn_obj *
GRN_TABLE_EACH(ctx, table2, 0, 0, id, &key, &key_size, &value2, {
if (grn_table_add_v_inline(ctx, table1, key, key_size, &value1, &added)) {
if (added) {
memcpy(value1, value2, value_size);
grn_memcpy(value1, value2, value_size);
} else {
grn_rset_recinfo *ri1 = value1;
grn_rset_recinfo *ri2 = value2;
Expand All @@ -3799,7 +3799,7 @@ grn_table_setoperation(grn_ctx *ctx, grn_obj *table1, grn_obj *table2, grn_obj *
} else {
GRN_TABLE_EACH(ctx, table2, 0, 0, id, &key, &key_size, &value2, {
if (grn_table_add_v_inline(ctx, table1, key, key_size, &value1, NULL)) {
memcpy(value1, value2, value_size);
grn_memcpy(value1, value2, value_size);
}
});
}
Expand Down Expand Up @@ -3831,7 +3831,7 @@ grn_table_setoperation(grn_ctx *ctx, grn_obj *table1, grn_obj *table2, grn_obj *
case GRN_OP_ADJUST :
GRN_TABLE_EACH(ctx, table2, 0, 0, id, &key, &key_size, &value2, {
if (grn_table_get_v(ctx, table1, key, key_size, &value1)) {
memcpy(value1, value2, value_size);
grn_memcpy(value1, value2, value_size);
}
});
break;
Expand Down Expand Up @@ -3880,7 +3880,7 @@ grn_obj_column_(grn_ctx *ctx, grn_obj *table, const char *name, unsigned int nam
if (len) {
buf[len++] = GRN_DB_DELIMITER;
if (len + name_size <= GRN_TABLE_MAX_KEY_SIZE) {
memcpy(buf + len, name, name_size);
grn_memcpy(buf + len, name, name_size);
column = grn_ctx_get(ctx, buf, len + name_size);
} else {
ERR(GRN_INVALID_ARGUMENT, "name is too long");
Expand Down Expand Up @@ -4026,7 +4026,7 @@ grn_column_create(grn_ctx *ctx, grn_obj *table,
goto exit;
}
fullname[len] = GRN_DB_DELIMITER;
memcpy(fullname + len + 1, name, name_size);
grn_memcpy(fullname + len + 1, name, name_size);
name_size += len + 1;
} else {
ERR(GRN_FUNCTION_NOT_IMPLEMENTED,
Expand Down Expand Up @@ -4154,7 +4154,7 @@ grn_column_open(grn_ctx *ctx, grn_obj *table,
goto exit;
}
fullname[len] = GRN_DB_DELIMITER;
memcpy(fullname + len + 1, name, name_size);
grn_memcpy(fullname + len + 1, name, name_size);
name_size += len + 1;
} else {
ERR(GRN_INVALID_ARGUMENT, "todo : not supported yet");
Expand Down Expand Up @@ -4218,7 +4218,7 @@ default_column_set_value(grn_ctx *ctx, grn_proc_ctx *pctx, grn_obj *in, grn_obj
ERR(GRN_NO_MEMORY_AVAILABLE, "ra get failed");
return GRN_NO_MEMORY_AVAILABLE;
}
memcpy(v, in->u.p.ptr, value_size);
grn_memcpy(v, in->u.p.ptr, value_size);
grn_ra_unref(ctx, (grn_ra *)pctx->obj, arg->id);
}
break;
Expand Down Expand Up @@ -6604,13 +6604,13 @@ grn_obj_set_value_column_fix_size(grn_ctx *ctx, grn_obj *obj, grn_id id,
} else {
void *b;
if ((b = GRN_CALLOC(element_size))) {
memcpy(b, v, s);
memcpy(p, b, element_size);
grn_memcpy(b, v, s);
grn_memcpy(p, b, element_size);
GRN_FREE(b);
}
}
} else {
memcpy(p, v, s);
grn_memcpy(p, v, s);
}
rc = GRN_SUCCESS;
break;
Expand Down Expand Up @@ -7384,7 +7384,7 @@ grn_hook_unpack(grn_ctx *ctx, grn_db_obj *obj, const char *buf, uint32_t buf_siz
new->proc = NULL;
}
if ((new->hld_size = hld_size)) {
memcpy(NEXT_ADDR(new), p, hld_size);
grn_memcpy(NEXT_ADDR(new), p, hld_size);
p += hld_size;
}
*last = new;
Expand Down Expand Up @@ -7618,7 +7618,7 @@ grn_obj_set_info_source_update(grn_ctx *ctx, grn_obj *obj, grn_obj *value)
if (!v2) {
return ctx->rc;
}
memcpy(v2, v, s);
grn_memcpy(v2, v, s);
if (DB_OBJ(obj)->source) { GRN_FREE(DB_OBJ(obj)->source); }
DB_OBJ(obj)->source = v2;
DB_OBJ(obj)->source_size = s;
Expand Down Expand Up @@ -7838,7 +7838,7 @@ grn_obj_add_hook(grn_ctx *ctx, grn_obj *obj, grn_hook_entry entry,
new->proc = (grn_proc *)proc;
new->hld_size = hld_size;
if (hld_size) {
memcpy(NEXT_ADDR(new), hld_value, hld_size);
grn_memcpy(NEXT_ADDR(new), hld_value, hld_size);
}
for (i = 0; i != offset && *last; i++) { last = &(*last)->next; }
new->next = *last;
Expand Down Expand Up @@ -8495,7 +8495,7 @@ grn_table_rename(grn_ctx *ctx, grn_obj *table, const char *name, unsigned int na
if (!(rc = grn_obj_rename(ctx, table, name, name_size))) {
grn_id *key;
char fullname[GRN_TABLE_MAX_KEY_SIZE];
memcpy(fullname, name, name_size);
grn_memcpy(fullname, name, name_size);
fullname[name_size] = GRN_DB_DELIMITER;
GRN_HASH_EACH(ctx, cols, id, &key, NULL, NULL, {
grn_obj *col = grn_ctx_at(ctx, *key);
Expand Down Expand Up @@ -8536,7 +8536,7 @@ grn_column_rename(grn_ctx *ctx, grn_obj *column, const char *name, unsigned int
goto exit;
}
fullname[len] = GRN_DB_DELIMITER;
memcpy(fullname + len + 1, name, name_size);
grn_memcpy(fullname + len + 1, name, name_size);
name_size += len + 1;
rc = grn_obj_rename(ctx, column, fullname, name_size);
}
Expand Down Expand Up @@ -8678,7 +8678,7 @@ grn_db_obj_init(grn_ctx *ctx, grn_obj *db, grn_id id, grn_db_obj *obj)
NULL,\
NULL);\
if (size > PATH_MAX) { ERR(GRN_FILENAME_TOO_LONG, "too long path"); }\
memcpy(buffer, path, size);\
grn_memcpy(buffer, path, size);\
buffer[size] = '\0';\
} else {\
gen_pathname(grn_obj_io(s->keys)->path, buffer, id); \
Expand All @@ -8700,7 +8700,7 @@ grn_db_obj_init(grn_ctx *ctx, grn_obj *db, grn_id id, grn_db_obj *obj)
NULL);\
if (size) {\
if ((r->source = GRN_MALLOC(size))) {\
memcpy(r->source, p, size);\
grn_memcpy(r->source, p, size);\
r->source_size = size;\
}\
}\
Expand Down Expand Up @@ -9308,7 +9308,7 @@ grn_column_name(grn_ctx *ctx, grn_obj *obj, char *namebuf, int buf_size)
}
len = pe - p0;
if (len && len <= buf_size) {
memcpy(namebuf, p0, len);
grn_memcpy(namebuf, p0, len);
}
}
}
Expand Down Expand Up @@ -9354,7 +9354,7 @@ grn_column_name(grn_ctx *ctx, grn_obj *obj, char *namebuf, int buf_size)
if (name) {
len = strlen(name);
if (len <= buf_size) {
memcpy(namebuf, name, len);
grn_memcpy(namebuf, name, len);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/expr.c
Expand Up @@ -3611,9 +3611,9 @@ put_logical_op(grn_ctx *ctx, scan_info **sis, int *ip, grn_operator op, int star
} else {
s_->flags &= ~SCAN_PUSH;
s_->logical_op = op;
memcpy(&sis[i], &sis[j], sizeof(scan_info *) * (r - j));
grn_memcpy(&sis[i], &sis[j], sizeof(scan_info *) * (r - j));
memmove(&sis[j], &sis[r], sizeof(scan_info *) * (i - r));
memcpy(&sis[i + j - r], &sis[i], sizeof(scan_info *) * (r - j));
grn_memcpy(&sis[i + j - r], &sis[i], sizeof(scan_info *) * (r - j));
}
break;
}
Expand Down

0 comments on commit e277e1e

Please sign in to comment.