Skip to content

Instantly share code, notes, and snippets.

View cloudwu's full-sized avatar

云风 cloudwu

View GitHub Profile
@cloudwu
cloudwu / luavector.c
Created February 21, 2017 03:46
Direct access vector in lua from C
#include <lua.h>
#include <lauxlib.h>
#include <ltable.h>
#include <lstate.h>
#include <lobject.h>
#include <stdio.h>
#include <string.h>
#define MAGIC_VECTOR 1.23456789
@cloudwu
cloudwu / utf8to16.c
Created January 5, 2017 06:41
A filter for convert utf8 to utf16 in windows.
// Use mingw in windows
// gcc -O2 -o utf8.exe utf8to16.c
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <wchar.h>
static const char trailingBytesForUTF8[256] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
@cloudwu
cloudwu / compat_alloc.c
Last active May 15, 2020 14:56
A compat memory alloc for lua
// gcc -g -Wall -o alloc alloc.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
#include <windows.h>
@cloudwu
cloudwu / lmd5.c
Last active November 30, 2018 13:56
lua md5 mod
/**
* $Id: md5.c,v 1.2 2008/03/24 20:59:12 mascarenhas Exp $
* Hash function MD5
* @author Marcela Ozorio Suarez, Roberto I.
*/
#include <string.h>
//#include "md5.h"
@cloudwu
cloudwu / deepcopy.lua
Created October 12, 2016 07:21
deep copy lua table
-- deepcopy a lua table, no recursion sub table, the key never be a table.
local function _copy(obj, target)
local n = 0
-- clear target table
for k,v in pairs(target) do
if type(v) == "table" then
v.__del = true
n = n + 1
else
@cloudwu
cloudwu / float.c
Created October 10, 2016 05:25
float
#include <stdio.h>
int
main() {
double f = 1.0/3.0;
double f2;
char tmp[100];
sprintf(tmp, "%lf", f);
sscanf(tmp, "%lf", &f2);
printf("f = %lf f2 = %lf , %d\n", f, f2, (f2 == f)); // should be 0 (false)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
struct memnode {
struct memnode * next;
};
struct fixalloc {
@cloudwu
cloudwu / testsocket.lua
Created May 18, 2016 10:51
skynet socket transfor
local skynet = require "skynet"
local socket = require "socket"
local mode , id = ...
local function echo(id)
socket.start(id)
skynet.error("start", id)
while true do
@cloudwu
cloudwu / memlog.lua
Last active July 27, 2017 12:15
Convert malloc log
local yield = coroutine.yield
local function loadlog(filename)
-- local log = {}
local f = assert(io.open(filename))
for line in f:lines() do
if line == "=======" then
break
end
local ptr, osize, nsize, ret = line:match("([^ ]+) (%d+) (%d+) ([^ ]+)")
@cloudwu
cloudwu / searchpath.lua
Created November 6, 2015 05:31
Lua version package.searchpath
local DIRSEP, PATSEP, MARK = package.config:match "(.-)\n(.-)\n(.-)\n"
MARK = MARK:gsub(".", function (c) return "%" .. c end)
-- todo: replace your own readable function
local function readable(filename)
local f = io.open(filename)
if f then
f:close()
return true
end