https://www.gravatar.com/avatar/a1a5f762bca516ff1fe40d39ac1e4f67?s=240&d=mp

Guanlan Dai

Libevent Analysis Notes (3) - Determining I/O Multiplexing Mechanism

Libevent’s original intention was to design a cross-platform lightweight I/O framework. Due to historical issues, the I/O multiplexing mechanisms across different platforms are difficult to unify. Therefore, the methods for handling cross-platform compatibility deserve special attention.

eventop is defined in the source code as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
static const struct eventop *eventops[]={

#ifdef HAVE_EVENT_PORTS

         &evportops,

#endif 

…

}

As can be seen, libevent uses macros to find available multiplexing mechanisms at compile time.

Libevent Analysis Notes (1) - Installation and Usage

Introduction to Libevent

/img/None.gifThe libevent API provides a mechanism to execute a callback function when a specific event occurs on a file descriptor or after a timeout has been reached. Furthermore, libevent also support callbacks due to signals or regular timeouts. The libevent API provides such a mechanism:
Execute a specified callback function when a specific event occurs in a specified file descriptor, or when a timeout is reached.
Additionally, libevent callback functionality also supports triggering by signals or regular timeouts.
Note that its license is a BSD-style license, which can be used in commercial products without having to open source.

Libevent Learning Notes (2) - Basic Data Structures

2.1 event_base Core Event Base Data Structure

/img/libevent1.jpg

As can be seen, event_base is the core part of the entire libevent, which consists of three structures: a time heap (corresponding to EVLIST_TIMEOUT), a registered queue (corresponding to EVLIST_INSERTE), and an active event queue (corresponding to EVLIST_ACTIVE).

The time heap uses min-Heap (minimum binary heap), while both the registered queue and active event queue use doubly linked lists.

Memcached Source Code Analysis Notes PDF

Memcached is a free, open source, high-performance, distributed memory object caching system, aimed at speeding up dynamic web applications by alleviating database load.

As the saying goes, the palest ink is better than the best memory.
This document is what I use to record some experiences during the process of reading Memcached source code, as well as analyzing some internal working mechanisms of memcached.
I hope it can also bring convenience to everyone.

A 'Strange Phenomenon' in C/C++ Arrays

Everyone is familiar with using arrays, right?
Take a look at this program, it’s quite simple.

1
2
3
4
5
6
7
8
#include<iostream>
int main()
{
    int a[] = {1,2,3,4,5};
    for(int i = 0 ; i < 5; i++)
        std::cout << i[a] << " ";
    return 0;
}

Now look carefully at line 6.
What did you notice?
Try compiling it to see if it passes?

Let’s simplify this program even more

1
2
int a[5] = {1,2,3,4,5};
int b = 1[a];

Now let’s look at the generated assembly code