Skip to content

Commit

Permalink
documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
tezc committed Dec 27, 2020
1 parent aac045c commit 8734d6d
Show file tree
Hide file tree
Showing 13 changed files with 310 additions and 26 deletions.
32 changes: 32 additions & 0 deletions memory-map/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,36 @@
### Mmap wrapper

- Basic mmap wrapper for Unix and Windows.
- File is expanded automatically depending on init flags.
- Just copy <b>sc_mmap.h</b> and <b>sc_mmap.c</b> to your project.

```c

#include "sc_mmap.h"

#include <assert.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
int rc;
struct sc_mmap mmap;

rc = sc_mmap_init(&mmap, "x.txt", O_RDWR | O_CREAT | O_TRUNC,
PROT_READ | PROT_WRITE, MAP_SHARED, 0, 15000);
assert(rc == 0);

void* ptr = mmap.ptr;
size_t mapped_len = mmap.len;

printf("mapped len : %zu \n", mapped_len);

*(char*)ptr = 't';

sc_mmap_msync(&mmap, 0, 4096);
sc_mmap_term(&mmap);

return 0;
}
```
19 changes: 19 additions & 0 deletions memory-map/mmap_example.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@

#include "sc_mmap.h"

#include <assert.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
int rc;
struct sc_mmap mmap;

rc = sc_mmap_init(&mmap, "x.txt", O_RDWR | O_CREAT | O_TRUNC,
PROT_READ | PROT_WRITE, MAP_SHARED, 0, 15000);
assert(rc == 0);

void* ptr = mmap.ptr;
size_t mapped_len = mmap.len;

printf("mapped len : %zu \n", mapped_len);

*(char*)ptr = 't';

sc_mmap_msync(&mmap, 0, 4096);
sc_mmap_term(&mmap);

return 0;
}
37 changes: 37 additions & 0 deletions mutex/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Mutex

### Mutex wrapper

- Basic mutex wrapper for Unixes and Windows.
- Just copy <b>sc_mutex.h</b> and <b>sc_mutex.c</b> to your project.

```c

#include "sc_mmap.h"

#include <assert.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
int rc;
struct sc_mmap mmap;

rc = sc_mmap_init(&mmap, "x.txt", O_RDWR | O_CREAT | O_TRUNC,
PROT_READ | PROT_WRITE, MAP_SHARED, 0, 15000);
assert(rc == 0);

void* ptr = mmap.ptr;
size_t mapped_len = mmap.len;

printf("mapped len : %zu \n", mapped_len);

*(char*)ptr = 't';

sc_mmap_msync(&mmap, 0, 4096);
sc_mmap_term(&mmap);

return 0;
}
```
12 changes: 2 additions & 10 deletions mutex/sc_mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
#define SC_MUTEX_H

#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#include <windows.h>
#else
#include <pthread.h>
#include <pthread.h>
#endif

struct sc_mutex
Expand All @@ -45,12 +45,4 @@ int sc_mutex_term(struct sc_mutex *mtx);
void sc_mutex_lock(struct sc_mutex *mtx);
void sc_mutex_unlock(struct sc_mutex *mtx);

/**
* If you want to log or abort on errors like mutex init,
* put your error function here. It will be called with printf like error msg.
*
* my_on_error(const char* fmt, ...);
*/
#define sc_mutex_on_error(...)

#endif
46 changes: 46 additions & 0 deletions option/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Option

### Portable getopt alternative

- Very simple alternative to getopt.
- Just copy <b>sc_opt.h</b> and <b>sc_opt.c</b> to your project.

```c

#include "sc_option.h"

static struct sc_option_item options[] = {{.letter = 'm', .name = NULL},
{.letter = 'k', .name = "key"},
{.letter = 'h', .name = "help"}};

int main(int argc, char *argv[])
{
char *value;

struct sc_option opt = {.argv = argv,
.count = sizeof(options) / sizeof(options[0]),
.options = options};

for (int i = 1; i < argc; i++) {
char c = sc_option_at(&opt, i, &value);
switch (c) {
case 'm':
// If value does not exist, it will point to '\0' character.
printf("Option 'm', value : %s \n", value);
break;
case 'k':
printf("Option 'k', value : %s \n", value);
break;
case 'h':
printf("Option 'h', value : %s \n", value);
break;
case '?':
printf("Unknown option : %s \n", argv[i]);
break;
}
}


return 0;
}```

4 changes: 2 additions & 2 deletions option/sc_option.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ char sc_option_at(struct sc_option *opt, int index, char **value)
char *pos;
const char *curr, *name;


pos = opt->argv[index];
*value = NULL;

Expand All @@ -44,7 +43,8 @@ char sc_option_at(struct sc_option *opt, int index, char **value)
pos++; // Skip first '-'
if (*pos != '-') {
for (int i = 0; i < opt->count; i++) {
if (*pos == opt->options[i].letter) {
if (*pos == opt->options[i].letter &&
strchr("= \0", *(pos + 1)) != NULL) {
id = *pos;
pos++; // skip letter
*value = pos + (*pos != '=' ? 0 : 1);
Expand Down
1 change: 0 additions & 1 deletion option/sc_option.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
* SOFTWARE.
*/
#ifndef SC_OPT_H

#define SC_OPT_H

#include <stdbool.h>
Expand Down
81 changes: 81 additions & 0 deletions perf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Perf benchmark

### Overview

- Using <i>perf_event_open</i> to get hardware and software counters.
- Only useful when you want to measure something inside the code really quick.
Otherwise, use <i>perf</i> itself.
- Linux only.
- All predefined hardware and software counters are generated in the header
file, you can uncomment counters as you wish.
- If some counters or combination of counters doesnt work or doesn't work as
expected, please check out performance counter register allocation
documentation for your CPU. CPUs have limited registers for performance
counters and some counters can use specific registers only.
- Just copy <b>sc_perf.h</b> and <b>sc_perf.c</b> to your project.

#### Usage

```c

#include "sc_perf.h"

int main(int argc, char *argv[])
{
sc_perf_start();

long_running_operation();

sc_perf_end();

return 0;
}

```
##### Output will be like
```
| Event | Value | Measurement time
---------------------------------------------------------------
| time (seconds) | 0.66 | (100,00%)
| cpu-clock | 654075766.00 | (100.00%)
| task-clock | 654077198.00 | (100.00%)
| page-faults | 3.00 | (100.00%)
| context-switches | 46.00 | (100.00%)
| cpu-migrations | 0.00 | (100.00%)
| page-fault-minor | 3.00 | (100.00%)
| cpu-cycles | 2656529748.00 | (100.00%)
| instructions | 7589235720.00 | (100.00%)
| cache-misses | 28715.00 | (100.00%)
| L1D-read-miss | 34124.00 | (100.00%)
| L1I-read-miss | 121958.00 | (100.00%)
```
##### Pause example
```c
#include "sc_perf.h"
int main(int argc, char *argv[])
{
sc_perf_start();
long_running_operation();
//Will stop counters.
sc_perf_pause();
operation_you_dont_want_to_measure();
//Start counters again.
sc_perf_start();
another_long_running_operation();
sc_perf_end();
sc_perf_start();
some_other_long_running_operation();
sc_perf_end();
return 0;
}
```
72 changes: 72 additions & 0 deletions queue/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Generic queue

#### Overview

- Type generic queue which grows when you add elements.
- Add/remove from head/tail is possible so it can be used as list, stack,
queue, dequeue etc.
- Single allocation for all the data.
- Keeps separate first and last element indexes, when you remove an element,
it doesn't move elements to fill the space.
- Just copy <b>sc_queue.h</b> and <b>sc_queue.c</b> to your project.


##### Usage


```c
#include "sc_queue.h"

#include <stdio.h>

int main(int argc, char *argv[])
{
int *queue;
int elem;

sc_queue_create(queue, 0);

sc_queue_add_last(queue, 2);
sc_queue_add_last(queue, 3);
sc_queue_add_last(queue, 4);
sc_queue_add_first(queue, 1);

sc_queue_foreach (queue, elem) {
printf("elem = [%d] \n", elem);
}

elem = sc_queue_del_last(queue);
printf("Last element was : [%d] \n", elem);

elem = sc_queue_del_first(queue);
printf("First element was : [%d] \n", elem);

sc_queue_destroy(queue);

return 0;
}
```
##### Note
Queue pointer is not stable, it may change if we expand the memory. If you
pass the queue to another function which can add items, do it by passing
reference of the queue pointer.
```c
void some_function_to_add_elems(long **q)
{
sc_queue_add_last(*q, 500);
}
int main(int argc, char *argv[])
{
long *q;
sc_queue_create(q, 0);
sc_queue_add_last(q, 300);
some_function_to_add_elems(&q);
sc_array_destroy(q);
}
```
4 changes: 2 additions & 2 deletions queue/queue_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ int main(int argc, char *argv[])
printf("elem = [%d] \n", elem);
}

elem = sc_queue_remove_last(queue);
elem = sc_queue_del_last(queue);
printf("Last element was : [%d] \n", elem);

elem = sc_queue_remove_first(queue);
elem = sc_queue_del_first(queue);
printf("First element was : [%d] \n", elem);

sc_queue_destroy(queue);
Expand Down
Loading

0 comments on commit 8734d6d

Please sign in to comment.