Coder Social home page Coder Social logo

tezc / sc Goto Github PK

View Code? Open in Web Editor NEW
2.2K 42.0 237.0 790 KB

Common libraries and data structures for C.

License: BSD 3-Clause "New" or "Revised" License

CMake 15.15% C 84.85%
c data-structures algorithms hashmap queue timer vector linked-list logger heap

sc's Introduction

Overview

License: BSD codecov

Portable, stand-alone C libraries and data structures. (C99)

Each folder is stand-alone with a single header/source pair in it. There is no
build for libraries, just copy files you want.

e.g., If you want the logger, copy sc_log.h and sc_log.c to your project.

Features

  • High performance & minimal memory usage
  • Portability between many operating systems and architectures
  • Tests with 100% branch coverage and multiple sanitizers
  • Drag & drop source code distribution

Test

There is 100% branch-coverage on Linux and CI runs on

OS         : Linux, MacOS, FreeBSD and Windows  
Compilers  : GCC, Clang, MSVC  
Arch       : x64, aarch64, armv6(32 bit), armv7(32 bit), ppc64le, s390x(big endian), riscv64  
Sanitizers : valgrind and clang/gcc sanitizers(address, undefined, thread)

List

Library Description
array Generic array/vector
buffer Buffer for encoding/decoding variables, best fit for protocol/serialization implementations
condition Condition wrapper for Posix and Windows
crc32 Crc32c, uses crc32c CPU instruction if available
heap Min heap which can be used as max heap/priority queue as well
ini Ini parser
linked list Intrusive linked list
logger Logger
map A high performance open addressing hashmap
memory map Mmap wrapper for Posix and Windows
mutex Mutex wrapper for Posix and Windows
option Cmdline argument parser. Very basic one
perf Benchmark utility to get performance counters info via perf_event_open()
queue Generic queue which can be used as dequeue/stack/list as well
sc Utility functions
signal Signal safe snprintf & Signal handler (handling CTRL+C, printing backtrace on crash etc)
socket Pipe / tcp sockets(also unix domain sockets) /Epoll/Kqueue/WSAPoll for Posix and Windows
string Length prefixed, null terminated C strings.
thread Thread wrapper for Posix and Windows.
time Time and sleep functions for Posix and Windows
timer Hashed timing wheel implementation with fast poll / cancel ops
uri A basic uri parser

Q&A


  • Is it any better than library X ?
    I often use these libraries for high performance server-side applications. Also,
    I care about readable and easy to debug code. In summary, these libraries show
    my taste(trade-offs) about performance/api-design/readability. You may or may
    not like it.

  • Why don't you change API here at X, so it will be easier to use?
    Send a pull request please but be sure you don't introduce an undefined
    behavior. It's possible to provide better APIs, especially to generic libraries,
    if you don't care about undefined behaviors. I try to avoid it.

  • What is the most efficient way to use these libraries?
    Just like any other code. Add to your project as source files and ideally use
    -O3 -flto + PGO. It may not make any difference for your use case though.

  • Is library X being used in any product?
    Some libraries are used in the production but please always test yourself.

  • Is there any release?
    Please use the master branch. It's considered stable.

  • Will you keep API stable?
    Please don't expect a stable API. These libraries are quite
    small (most of them are less than a few hundreds lines of code) and ideally you
    are supposed to read the code and understand what it does and adapt it to your
    needs. So, you should not update libraries blindly. I expect you to handle
    any possible API differences easily. That being said, I'll do my best to keep
    API stable.

sc's People

Contributors

rafaeldelboni avatar svladykin avatar tezc avatar yasemill avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sc's Issues

Using this lib over FetchContent

I'm trying to use your libs over cmake's FetchContent like so:

# Download / Include Commons libs SC
FetchContent_Declare(
  sc_lib
  GIT_REPOSITORY https://github.com/tezc/sc.git
  GIT_TAG        master)

FetchContent_MakeAvailable(sc_lib)

Then when I try to link the sc_str in my build

target_link_libraries(
  disassembler
  constants
  sc_str
)

I get this error:

...

CMake Error at src/CMakeLists.txt:11 (target_link_libraries):
  Target "sc_str" of type EXECUTABLE may not be linked into another target.
  One may link only to INTERFACE, OBJECT, STATIC or SHARED libraries, or to
  executables with the ENABLE_EXPORTS property set.
...

Any plans on implementing AVL tree?

I was in need of a simple string map data structure that would be alphabetically sorted, however the only map implementation in here is a simple hash map. I was wondering if there are any plans to implement an ordered map in the form of an AVL tree? I ended up using another random implementation I found on github though I would've loved to use this library since it's awesome and I use it for everything else.

#if defined(_WIN32) || defined(_WIN64) is not enough to check for MSVC

For compatibility reason I think, MinGW-W64 also defined these macros. To be sure it's actually MSVC, use this:

#if defined(_WIN32) || defined(_WIN64)
#if defined(_MSC_VER)
#pragma warning(disable : 4996)
#endif
#endif

MinGW-W64 doesn't understand that pragma. BTW, even if we applied this additional check I can't get sc_log.c to compile. It seems you only have MSVC in mind when you wrote this library, don't you?

sc_str_append_fmt is trimming whitespaces at the end of the formated strings

This is intended or is there something I'm missing?

Steps to reproduce:

Make a new test on string/str_test.c

void test7()
{
    char* s1;
    char* instruction = "RTS";
    s1 = sc_str_create(NULL);
    sc_str_append_fmt(&s1, "%s ", instruction);
    assert(sc_str_cmp(s1,"RTS "));
}

Error log:

17: sc_str_test: ./sc/string/str_test.c:516: test7: Assertion `sc_str_cmp(s1,"RTS ")' failed.
17/21 Test #17: sc_str_test ......................Subprocess aborted***Exception:   0.07 sec
sc_str_test: ./sc/string/str_test.c:516: test7: Assertion `sc_str_cmp(s1,"RTS ")' failed.

What does this function?

@tezc
sc_map_remap_
???
What is the better way update memory usage after many attemtps with deliting and adding keys and values?
This time i try to use this sheme

sc_map_init_64s(&tmp, 0, 0);
            sc_map_foreach (&map, key, value){
                sc_map_put_64s(&tmp, key, value);
            }
            sc_map_term_64s(&map);
            sc_map_init_64s(&map, 0, 0);
            sc_map_foreach (&tmp, key, value){
                sc_map_put_64s(&map, key, value);
            }
            sc_map_term_64s(&tmp);

but maby exists better way?

question: why pass by reference in `sc_str_substring((char** str, uint32_t start, uint32_t end)`?

I'd say I am still new to learning to code in C. This library has been really helpful for me to see how C code is written in the real world. So, thank you!

I was wondering why pass the str by reference in some cases sc_str_destroy() and sc_str_trim(), because I think it's possible to work with pointers without it. For example:

void alter(char* str) {
  str[0] = 'f';
  free(str);
}

int main() {
  char* str = malloc(6);
  strcpy(str, "hello");
  alter(str);
}

sc_sock thread-safety

I'm going to have a multi-threaded system with multiple poll objects and poller threads. I want to be able to add/del sockets to a poll object from multiple threads (e.g. server accepts a non-blocking socket in one thread and dispatches it to a random poller thread, or a worker thread does non-blocking socket connect and dispatches the socket to a random poller thread as well).

As far as I can see epoll and kqueue already support this out of the box. Still sc_sock_poll has some additional unprotected shared state:

  1. Resizeable sc_sock_poll.events (and count + cap). This is fixable by removing resizing and having events buffer of a constant size (like 4096 or so).
  2. Shared sc_sock_poll.err. Looks like the right way to handle it is to fill sc_sock.err instead of sc_sock_poll.err on add/del failures, but this is a backward compatibility breaking change (in a minor way though, it breaks only error handling path and not critically: the outdated code will see empty error message in sc_sock_poll_err() after failed add/del operations, which does not look too bad).

Windows as usual would require many more situps but still must be doable :)

@tezc What do you think about it? I can implement this, are you OK with reviewing/merging such a change?

[error]“afunix.h”: No such file or directory sc_socket_test

image
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C1083 无法打开包括文件: “afunix.h”: No such file or directory sc_socket_test F:\workspace\project\github\cpp\sc\socket\sc_sock.c 43
错误 C1083 无法打开包括文件: “afunix.h”: No such file or directory sc_socket_test F:\workspace\project\github\cpp\sc\socket\sc_sock.c 43

A little problem about hashmap

Hello. I don't understand what's the function of field "used" in the struct sc_map_xxx.

struct sc_map_str {
	struct sc_map_item_str *mem;
	uint32_t cap;
	uint32_t size;
	uint32_t load_fac;
	uint32_t remap;
	_Bool used; // what is this used for?
	_Bool oom;
	_Bool found;
};

Compile errors on Release

I'm having some errors when compiling this project as Release:

Steps to reproduce

mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j

Errors log:

stderr.txt

I would like to give the credit to @lucascebertin that point out this "bug"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.