entouched upse_memcpy(), qse_memset().

added some code for urs
This commit is contained in:
2014-08-26 16:29:56 +00:00
parent 187443bb1d
commit 0251aef726
4 changed files with 279 additions and 67 deletions

View File

@ -85,8 +85,91 @@ void* qse_memcpy (void* dst, const void* src, qse_size_t n)
while (n-- > 0) *d++ = *s++;
return dst;
#else
#elif defined(__GNUC__) && (defined(__x86_64) || defined(__amd64))
/* i don't really care about alignments for x86-64 at this moment. fix it later */
__asm__ volatile (
"cld\n\t"
"rep movsq\n"
: /* no output */
:"D" (dst), "S" (src), "c" (n >> 3) /* input: %rdi = d, %rsi = src, %rcx = n / 8 */
:"memory"
);
__asm__ volatile (
"rep movsb\n"
: /* no output */
:"c" (n & 7) /* %rcx = n % 8, use existing %rdi and %rsi */
:"memory", "%rdi", "%rsi"
);
return dst;
#if 0
qse_byte_t* d = dst;
__asm__ volatile (
"cld\n\t"
"rep movsq\n"
: "=D" (d), "=S" (src) /* output: d = %rdi, src = %rsi */
:"0" (d), "1" (src), "c" (n >> 3) /* input: %rdi = d, %rsi = src, %rcx = n / 8 */
:"memory"
);
__asm__ volatile (
"rep movsb"
: /* no output */
:"D" (d), "S" (src), "c" (n & 7) /* input: %rdi = d, %rsi = src, %rcx = n % 8 */
:"memory"
);
return dst;
#endif
#elif defined(__GNUC__) && (defined(__i386) || defined(i386))
/* i don't really care about alignments for x86 at this moment. fix it later */
__asm__ volatile (
"cld\n\t"
"rep movsl\n"
: /* no output */
:"D" (dst), "S" (src), "c" (n >> 2) /* input: %edi = d, %esi = src, %ecx = n / 8 */
:"memory"
);
__asm__ volatile (
"rep movsb\n"
: /* no output */
:"c" (n & 3) /* %rcx = n % 8, use existing %edi and %esi */
:"memory", "%edi", "%esi"
);
return dst;
#if 0
qse_byte_t* d = dst;
__asm__ volatile (
"cld\n\t"
"rep movsl\n"
:"=D" (d), "=S" (src) /* output: d = %edi, src = %esi */
:"0" (d), "1" (src), "c" (n >> 2) /* input: %edi = d, %esi = src, %ecx = n / 4 */
:"memory"
);
__asm__ volatile (
"rep movsb\n"
:
:"D" (d), "S" (src), "c" (n & 3) /* input: %edi = d, %esi = src, %ecx = n % 4 */
:"memory"
);
return dst;
#endif
#else
qse_byte_t* d;
qse_byte_t* s;
@ -207,8 +290,81 @@ void* qse_memset (void* dst, int val, qse_size_t n)
while (n-- > 0) *d++ = (qse_byte_t)val;
return dst;
#else
#elif defined(__GNUC__) && (defined(__x86_64) || defined(__amd64))
/* i don't really care about alignments for x86-64 at this moment. fix it later */
qse_byte_t* d = dst;
__asm__ volatile ("cld\n");
if (n >= 8)
{
qse_size_t qw = (qse_byte_t)val;
if (qw)
{
qw = (qw << 8) | (qse_byte_t)val;
qw = (qw << 8) | (qse_byte_t)val;
qw = (qw << 8) | (qse_byte_t)val;
qw = (qw << 8) | (qse_byte_t)val;
qw = (qw << 8) | (qse_byte_t)val;
qw = (qw << 8) | (qse_byte_t)val;
qw = (qw << 8) | (qse_byte_t)val;
}
__asm__ volatile (
"rep stosq\n"
:"=D" (d) /* output: d = %rdi */
:"0" (d), "a" (qw), "c" (n >> 3) /* input: %rdi = d, %rax = qw, %rcx = n / 8 */
:"memory"
);
}
__asm__ volatile (
"rep stosb\n"
: /* no output */
:"D" (d), "a" (val), "c" (n & 7) /* input: %rdi = d, %rax = src, %rcx = n % 8 */
:"memory"
);
return dst;
#elif defined(__GNUC__) && (defined(__i386) || defined(i386))
/* i don't really care about alignments for x86 at this moment. fix it later */
qse_byte_t* d = dst;
__asm__ volatile ("cld\n");
if (n >= 4)
{
qse_size_t dw = (qse_byte_t)val;
if (dw)
{
dw = (dw << 8) | (qse_byte_t)val;
dw = (dw << 8) | (qse_byte_t)val;
dw = (dw << 8) | (qse_byte_t)val;
}
__asm__ volatile (
"rep stosl\n"
:"=D" (d) /* output: d = %edi */
:"0" (d), "a" (dw), "c" (n >> 2) /* input: %edi = d, %eax = dw, %ecx = n / 4 */
:"memory"
);
}
__asm__ volatile (
"rep stosb\n"
: /* no output */
:"D" (d), "a" (val), "c" (n & 3) /* input: %edi = d, %eax = src, %ecx = n % 4 */
:"memory"
);
return dst;
#else
qse_byte_t* d;
qse_size_t rem;

View File

@ -36,7 +36,7 @@ struct urs_hdr_t
qse_uint16_t seq; /* in network-byte order */
qse_uint16_t rcode; /* response code */
qse_uint32_t urlsum; /* checksum of url in the request */
qse_uint16_t urllen; /* url length in network-byte order */
qse_uint16_t pktlen; /* packet header size + url length */
};
struct urs_pkt_t
@ -59,8 +59,8 @@ struct urs_ctx_t
urs_req_t* reqs[1024]; /* TOOD: choose the right size */
qse_uint16_t req_count;
qse_uint8_t rcvbuf[URS_MAX_URL_LEN + QSE_SIZEOF(urs_pkt_t)];
qse_uint8_t fmtbuf[URS_MAX_URL_LEN + QSE_SIZEOF(urs_pkt_t)];
qse_uint8_t rcvbuf[QSE_SIZEOF(urs_hdr_t) + URS_MAX_URL_LEN + 1];
qse_uint8_t fmtbuf[QSE_SIZEOF(urs_hdr_t) + URS_MAX_URL_LEN + 1];
};
struct urs_req_t
@ -95,7 +95,7 @@ static int urs_open (qse_httpd_t* httpd, qse_httpd_urs_t* urs)
qse_nwad_t nwad;
urs_ctx_t* dc;
httpd_xtn_t* httpd_xtn;
int type, proto = IPPROTO_SCTP;
int type, proto = IPPROTO_UDP; //IPPROTO_SCTP;
httpd_xtn = qse_httpd_getxtn (httpd);
@ -147,12 +147,14 @@ static int urs_open (qse_httpd_t* httpd, qse_httpd_urs_t* urs)
dc->urs_socket = QSE_INVALID_SCKHND;
}
#if 0
if (proto == IPPROTO_SCTP)
{
/* TODO: error ahndleing */
if (qse_isvalidsckhnd(urs->handle[0].i)) listen (urs->handle[0].i, 99);
if (qse_isvalidsckhnd(urs->handle[1].i)) listen (urs->handle[1].i, 99);
}
#endif
urs->handle_count = 2;
urs->ctx = dc;
@ -215,7 +217,7 @@ static int urs_recv (qse_httpd_t* httpd, qse_httpd_urs_t* urs, qse_ubi_t handle)
socklen_t fromlen;
qse_uint16_t xid;
qse_ssize_t len;
qse_ssize_t len, url_len;
urs_pkt_t* pkt;
urs_req_t* req;
@ -229,29 +231,34 @@ printf ("URS_RECV....\n");
/* TODO: check if fromaddr matches the dc->skad... */
pkt = (urs_pkt_t*)dc->rcvbuf;
if (len >= QSE_SIZEOF(pkt->hdr) && len >= QSE_SIZEOF(pkt->hdr) + qse_ntoh16(pkt->hdr.urllen))
if (len >= QSE_SIZEOF(urs_hdr_t))
{
xid = qse_ntoh16(pkt->hdr.seq) % QSE_COUNTOF(dc->reqs);
for (req = dc->reqs[xid]; req; req = req->next)
pkt->hdr.pktlen = qse_ntoh16(pkt->hdr.pktlen);
if (len == pkt->hdr.pktlen)
{
if (req->pkt->hdr.seq == pkt->hdr.seq && req->pkt->hdr.urlsum == pkt->hdr.urlsum)
url_len = pkt->hdr.pktlen - QSE_SIZEOF(urs_hdr_t);
xid = qse_ntoh16(pkt->hdr.seq) % QSE_COUNTOF(dc->reqs);
for (req = dc->reqs[xid]; req; req = req->next)
{
/* null-terminate the url for easier processing */
pkt->url[qse_ntoh16(pkt->hdr.urllen)] = QSE_MT('\0');
if (req->pkt->hdr.seq == pkt->hdr.seq && req->pkt->hdr.urlsum == pkt->hdr.urlsum)
{
/* null-terminate the url for easier processing */
pkt->url[url_len] = QSE_MT('\0');
urs_remove_tmr_tmout (httpd, req);
req->rewrite (httpd, req->pkt->url, pkt->url, req->ctx);
urs_remove_tmr_tmout (httpd, req);
req->rewrite (httpd, req->pkt->url, pkt->url, req->ctx);
/* detach the request off dc->reqs */
if (req == dc->reqs[xid]) dc->reqs[xid] = req->next;
else req->prev->next = req->next;
if (req->next) req->next->prev = req->prev;
/* detach the request off dc->reqs */
if (req == dc->reqs[xid]) dc->reqs[xid] = req->next;
else req->prev->next = req->next;
if (req->next) req->next->prev = req->prev;
qse_httpd_freemem (httpd, req);
dc->req_count--;
qse_httpd_freemem (httpd, req);
dc->req_count--;
break;
break;
}
}
}
}
@ -362,21 +369,19 @@ static int urs_send (qse_httpd_t* httpd, qse_httpd_urs_t* urs, const qse_mchar_t
xid = seq % QSE_COUNTOF(dc->reqs);
req = qse_httpd_callocmem (httpd, QSE_SIZEOF(*req) + url_len + QSE_SIZEOF(urs_pkt_t));
req = qse_httpd_callocmem (httpd, QSE_SIZEOF(*req) + QSE_SIZEOF(urs_hdr_t) + url_len + 1);
if (req == QSE_NULL) goto oops;
req->tmr_tmout = QSE_TMR_INVALID_INDEX;
req->seq = seq;
req->pkt = (urs_pkt_t*)(req + 1);
req->pktlen = QSE_SIZEOF(urs_hdr_t) + url_len;
req->pkt->hdr.seq = qse_hton16(seq);
req->pkt->hdr.urllen = qse_hton16(url_len);
req->pkt->hdr.pktlen = qse_hton16(req->pktlen);
req->pkt->hdr.urlsum = hash_string (url);
qse_mbscpy (req->pkt->url, url);
/* -1 to exclude the terminating '\0' as urs_pkt_t has url[1]. */
req->pktlen = QSE_SIZEOF(urs_pkt_t) + url_len - 1;
req->rewrite = rewrite;
req->ctx = ctx;

View File

@ -730,10 +730,11 @@ static qse_sck_hnd_t open_udp_socket (qse_httpd_t* httpd, int domain, int type,
if (set_socket_nonblock (httpd, fd, 1) <= -1) goto oops;
#if 1
if (proto == IPPROTO_SCTP)
{
struct sctp_initmsg im;
struct sctp_paddrparams hb;
QSE_MEMSET (&im, 0, QSE_SIZEOF(im));
im.sinit_num_ostreams = 1;
@ -741,7 +742,15 @@ static qse_sck_hnd_t open_udp_socket (qse_httpd_t* httpd, int domain, int type,
im.sinit_max_attempts = 1;
if (setsockopt (fd, SOL_SCTP, SCTP_INITMSG, &im, QSE_SIZEOF(im)) <= -1) goto oops;
QSE_MEMSET (&hb, 0, QSE_SIZEOF(hb));
hb.spp_flags = SPP_HB_ENABLE;
hb.spp_hbinterval = 5000;
hb.spp_pathmaxrxt = 1;
if (setsockopt (fd, SOL_SCTP, SCTP_PEER_ADDR_PARAMS, &hb, QSE_SIZEOF(hb)) <= -1) goto oops;
}
#endif
return fd;