Skip to content

Instantly share code, notes, and snippets.

View cpq's full-sized avatar
🎭
Купатися чи не купатись?

Sergey Lyubka cpq

🎭
Купатися чи не купатись?
View GitHub Profile
@cpq
cpq / chromatic_vs_natural.py
Created December 16, 2011 12:44
Chromatic vs natural scale
#!/usr/bin/env python
# Copyright (c) 2011 by Sergey Lyubka
# All rights reserved
#
# This program calculates note frequencies for chromatic and natural scale.
# Chromatic scale is built by splitting an octave (2x frequency bump) onto
# 12 equal intervals (semitones).
semitones = ['%.5f' % pow(pow(2, x), 1 / 12.0) for x in range(13)]
tones = [0, 2, 4, 5, 7, 9, 11] # Major scale
@cpq
cpq / embed.c
Last active March 4, 2024 10:45
How to embed data files into C/C++ executable
// Copyright (c) Sergey Lyubka, 2013.
// All rights reserved.
// Released under the MIT license.
// This program is used to embed arbitrary data into a C binary. It takes
// a list of files as an input, and produces a .c data file that contains
// contents of all these files as collection of char arrays.
// Usage:
// 1. Compile this file:
// cc -o embed embed.c
@cpq
cpq / integer.h
Created July 29, 2013 15:13
Big number integer arithmetic in a single header file
/*
* Large integer manipulation functions.
* All integers are assumed to be unsigned and represented as
* a contiguous array of bytes (unsigned char *ptr, int len)
* in little-endian byte order (ptr points to the least significant byte).
*/
#define HUGE 4096 /* Length of the biggest integer in bytes */
/*
@cpq
cpq / mjpg.c
Last active June 1, 2018 17:25
Example of mjpg streaming using mongoose web server.
// Copyright (c) 2013 Sergey Lyubka
// All rights reserved
//
// An example of mjpg streaming using mongoose web server.
// Usage:
// cc mjpg.c mongoose.c && ./a.out
// This code is public domain.
#include <sys/stat.h>
#include <string.h>
@cpq
cpq / quoted_string_parser.c
Created September 10, 2013 17:23
Igor Izvarin's question on parsing string
// Usage:
// cc -W -Wall ~/tmp/quoted_string_parser.c
// ./a.out " dwedewd, dede" " 'sds ee' ,'ddd'"
#include <stdio.h>
#include <string.h>
static const char *whitespaces = " \r\n";
static const char *whitespaces_or_comma = " \r\n,";
@cpq
cpq / Mutex.md
Last active May 14, 2023 18:15
What is a mutex and how does it work

What is a mutex and how does it work

Imagine a big office (your program), with many assets (shared resources) and many employees (threads). For example, all employees share one common resource - a toilet. They have agreed to use a label on a toilet's door (mutex).

When an employee wants to use the toilet he checks a label on a door (locks a mutex). If it is "engaged" he waits (blocks on a mutex), then when it is "free", he enters the toilet and changes the label to

@cpq
cpq / Hash.md
Created January 24, 2014 14:35
What is a hash and how does it work

What is a hash and how does it work

Let us start from a practical example. Imagine we are writing a traffic monitoring application. The purpose of application would be to calculate a number of bytes sent by each IP address. To do that, let create a structure that does that accounting:

struct ipstat {

uint32_t ip; /* Source IP address */

@cpq
cpq / Stack.md
Last active April 17, 2024 11:29
Why stack grows down

Why stack grows down

Any running process has several memory regions: code, read-only data, read-write data, et cetera. Some regions, such as code and read-only data, are static and do not change over time. Other regions are dynamic: they can expand and shrink. Usually there are two such regions: dynamic read-write data region, called heap, and a region called stack. Heap holds dynamic memory allocations, and stack is mostly used for keeping function frames.

Both stack and heap can grow. An OS doesn't know in advance whether stack or heap will be used predominantly. Therefore, an OS must layout these two memory regions in a way to guarantee maximum space for both. And here is the solution:

  1. Layout static memory regions at the edges of process's virtual memory
  2. Put heap and stack on edges too, and let them grow towards each other: one grows up, one grows down
@cpq
cpq / eval_arith.c
Last active August 29, 2015 14:05
Arithmetic expression evaluator. Demonstrates how to write recursive descent parser in C.
// Copyright (c) 2014 Sergey Lyubka. All rights reserved.
// Arithmetic expression evaluator.
//
// Compilation: cc -o eval_arith eval_arith.c
// Usage: ./eval_arith "2 - 3 * ( 4 - 2.7 * 11.5 ) + 1.78"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
@cpq
cpq / api.c
Created October 16, 2014 15:02
Extensible C API
#define END_OF_PARAMS (-1)
// Each extensible function is going to have it's own params enum
enum {
SEND_FILE_EXTRA_HEADER, // next arg is const char *
SEND_FILE_BYTE_RANGE, // next args are size_t, size_t
SEND_FILE_NUM_PARAMS
};
int mg_send_file(const char *path, ...) {