Skip to content

Instantly share code, notes, and snippets.

View cloudwu's full-sized avatar

云风 cloudwu

View GitHub Profile
-- main.lua
local cell = require "cell"
local function accepter(fd, addr, listen_fd)
-- can't read fd in this function, because socket.cell haven't forward data from fd
local client = cell.cmd("launch", "test.client",fd, addr)
-- return cell the data from fd will forward to, you can also return nil for forwarding to self
return client
end
@cloudwu
cloudwu / lzw
Created August 27, 2013 04:12
LZW 伪代码
Encode:
[.c.] = Empty;
[.c.]k = First Character in CharStream;
while ([.c.]k != EOF )
{
  if ( [.c.]k is in the StringTable)
  {
    [.c.] = [.c.]k;
  }
@cloudwu
cloudwu / ffr.c
Last active January 30, 2023 04:29
fast IEEE 754 float random
#include <stdint.h>
#include <stdlib.h>
// assert(RAND_MAX >= 0x7fff)
float
random_0_to_1() {
union {
uint32_t d;
float f;
} u;
@cloudwu
cloudwu / dh.c
Last active June 19, 2022 15:38
Diffie-Hellman Key Exchange
// The biggest 64bit prime
#define P 0xffffffffffffffc5ull
#define G 5
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <stdlib.h>
// calc a * b % p , avoid 64bit overflow
int
binary_search_first_position(int *A, int n, int target) {
int end[2] = { -1, n };
while (end[0] + 1 < end[1]) {
int mid = (end[0] + end[1]) / 2;
int sign = (unsigned)(A[mid] - target) >> 31;
end[1-sign] = mid;
}
int high = end[1];
if (high >= n || A[high] != target)
--[[ config
root = "./"
listen = "127.0.0.1:8786"
redisaddr = "127.0.0.1:6379[1]"
dbfile = root .. "backup.db"
thread = 4
logger = nil
harbor = 1
diff --git a/src/lapi.c b/src/lapi.c
index d011431..a4fce7f 100644
--- a/src/lapi.c
+++ b/src/lapi.c
@@ -993,6 +993,63 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
return status;
}
+static Proto *
+cloneproto (lua_State *L, const Proto *src) {
@cloudwu
cloudwu / loadfile.c
Created March 20, 2014 03:55
use lua_clonefunction for fast lua file load
// use clonefunction
#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {}
#define UNLOCK(q) __sync_lock_release(&(q)->lock);
struct codecache {
int lock;
lua_State *L;
};
local skynet = require "skynet"
local socket = require "socket"
local mode = ...
if mode == "agent" then
skynet.start(function()
skynet.dispatch("lua", function(_,_,id)
socket.start(id)
local skynet = require "skynet"
local mongo = require "mongo"
skynet.start(function()
local db = mongo.client { host = "127.0.0.1" , username = "user", password = "pass" }
local r = db:runCommand "listDatabases"
for k,v in ipairs(r.databases) do
print(v.name)
end