From c0f9e5be106fdea277b9bb47caea8f3fcd487def Mon Sep 17 00:00:00 2001 From: Eugene Wissner Date: Thu, 26 Apr 2018 10:23:06 +0200 Subject: [PATCH] Replace std min/max. Fix #35 --- source/tanya/async/event/epoll.d | 374 +++++++++++++++--------------- source/tanya/async/event/kqueue.d | 4 +- source/tanya/container/array.d | 3 +- source/tanya/container/list.d | 5 +- source/tanya/container/string.d | 3 +- source/tanya/math/mp.d | 5 +- source/tanya/memory/smartref.d | 12 +- source/tanya/network/socket.d | 2 +- 8 files changed, 206 insertions(+), 202 deletions(-) diff --git a/source/tanya/async/event/epoll.d b/source/tanya/async/event/epoll.d index f0020ac..718e5e8 100644 --- a/source/tanya/async/event/epoll.d +++ b/source/tanya/async/event/epoll.d @@ -1,187 +1,187 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -/** - * Event loop implementation for Linux. - * - * Copyright: Eugene Wissner 2016-2018. - * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, - * Mozilla Public License, v. 2.0). - * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) - * Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/event/epoll.d, - * tanya/async/event/epoll.d) - */ -module tanya.async.event.epoll; - -version (D_Ddoc) -{ -} -else version (linux): - -import core.stdc.errno; -public import core.sys.linux.epoll; -import core.sys.posix.unistd; -import core.time; -import std.algorithm.comparison; -import tanya.async.event.selector; -import tanya.async.loop; -import tanya.async.protocol; -import tanya.async.transport; -import tanya.async.watcher; -import tanya.container.array; -import tanya.memory; -import tanya.network.socket; - -extern (C) nothrow @nogc -{ - int epoll_create1(int flags); - int epoll_ctl (int epfd, int op, int fd, epoll_event *event); - int epoll_wait (int epfd, epoll_event *events, int maxevents, int timeout); -} - -final class EpollLoop : SelectorLoop -{ - protected int fd; - private Array!epoll_event events; - - /** - * Initializes the loop. - */ - this() @nogc - { - if ((fd = epoll_create1(EPOLL_CLOEXEC)) < 0) - { - throw defaultAllocator.make!BadLoopException("epoll initialization failed"); - } - super(); - events = Array!epoll_event(maxEvents); - } - - /** - * Frees loop internals. - */ - ~this() @nogc - { - close(fd); - } - - /** - * Should be called if the backend configuration changes. - * - * Params: - * watcher = Watcher. - * oldEvents = The events were already set. - * events = The events should be set. - * - * Returns: $(D_KEYWORD true) if the operation was successful. - */ - protected override bool reify(SocketWatcher watcher, - EventMask oldEvents, - EventMask events) @nogc - { - int op = EPOLL_CTL_DEL; - epoll_event ev; - - if (events == oldEvents) - { - return true; - } - if (events && oldEvents) - { - op = EPOLL_CTL_MOD; - } - else if (events && !oldEvents) - { - op = EPOLL_CTL_ADD; - } - - ev.data.fd = watcher.socket.handle; - ev.events = (events & (Event.read | Event.accept) ? EPOLLIN | EPOLLPRI : 0) - | (events & Event.write ? EPOLLOUT : 0) - | EPOLLET; - - return epoll_ctl(fd, op, watcher.socket.handle, &ev) == 0; - } - - /** - * Does the actual polling. - */ - protected override void poll() @nogc - { - // Don't block - immutable timeout = cast(immutable int) blockTime.total!"msecs"; - auto eventCount = epoll_wait(fd, events.get().ptr, maxEvents, timeout); - - if (eventCount < 0) - { - if (errno != EINTR) - { - throw defaultAllocator.make!BadLoopException(); - } - return; - } - - for (auto i = 0; i < eventCount; ++i) - { - auto transport = cast(StreamTransport) connections[events[i].data.fd]; - - if (transport is null) - { - auto connection = cast(ConnectionWatcher) connections[events[i].data.fd]; - assert(connection !is null); - - acceptConnections(connection); - } - else if (events[i].events & EPOLLERR) - { - kill(transport); - continue; - } - else if (events[i].events & (EPOLLIN | EPOLLPRI | EPOLLHUP)) - { - SocketException exception; - try - { - ptrdiff_t received; - do - { - received = transport.socket.receive(transport.output[]); - transport.output += received; - } - while (received); - } - catch (SocketException e) - { - exception = e; - } - if (transport.socket.disconnected) - { - kill(transport, exception); - continue; - } - else if (transport.output.length) - { - pendings.insertBack(transport); - } - } - if (events[i].events & EPOLLOUT) - { - transport.writeReady = true; - if (transport.input.length) - { - feed(transport); - } - } - } - } - - /** - * Returns: The blocking time. - */ - override protected @property inout(Duration) blockTime() - inout @safe pure nothrow - { - return min(super.blockTime, 1.dur!"seconds"); - } -} +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/** + * Event loop implementation for Linux. + * + * Copyright: Eugene Wissner 2016-2018. + * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, + * Mozilla Public License, v. 2.0). + * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) + * Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/event/epoll.d, + * tanya/async/event/epoll.d) + */ +module tanya.async.event.epoll; + +version (D_Ddoc) +{ +} +else version (linux): + +import core.stdc.errno; +public import core.sys.linux.epoll; +import core.sys.posix.unistd; +import core.time; +import tanya.algorithm.comparison; +import tanya.async.event.selector; +import tanya.async.loop; +import tanya.async.protocol; +import tanya.async.transport; +import tanya.async.watcher; +import tanya.container.array; +import tanya.memory; +import tanya.network.socket; + +extern (C) nothrow @nogc +{ + int epoll_create1(int flags); + int epoll_ctl (int epfd, int op, int fd, epoll_event *event); + int epoll_wait (int epfd, epoll_event *events, int maxevents, int timeout); +} + +final class EpollLoop : SelectorLoop +{ + protected int fd; + private Array!epoll_event events; + + /** + * Initializes the loop. + */ + this() @nogc + { + if ((fd = epoll_create1(EPOLL_CLOEXEC)) < 0) + { + throw defaultAllocator.make!BadLoopException("epoll initialization failed"); + } + super(); + events = Array!epoll_event(maxEvents); + } + + /** + * Frees loop internals. + */ + ~this() @nogc + { + close(fd); + } + + /** + * Should be called if the backend configuration changes. + * + * Params: + * watcher = Watcher. + * oldEvents = The events were already set. + * events = The events should be set. + * + * Returns: $(D_KEYWORD true) if the operation was successful. + */ + protected override bool reify(SocketWatcher watcher, + EventMask oldEvents, + EventMask events) @nogc + { + int op = EPOLL_CTL_DEL; + epoll_event ev; + + if (events == oldEvents) + { + return true; + } + if (events && oldEvents) + { + op = EPOLL_CTL_MOD; + } + else if (events && !oldEvents) + { + op = EPOLL_CTL_ADD; + } + + ev.data.fd = watcher.socket.handle; + ev.events = (events & (Event.read | Event.accept) ? EPOLLIN | EPOLLPRI : 0) + | (events & Event.write ? EPOLLOUT : 0) + | EPOLLET; + + return epoll_ctl(fd, op, watcher.socket.handle, &ev) == 0; + } + + /** + * Does the actual polling. + */ + protected override void poll() @nogc + { + // Don't block + immutable timeout = cast(immutable int) blockTime.total!"msecs"; + auto eventCount = epoll_wait(fd, events.get().ptr, maxEvents, timeout); + + if (eventCount < 0) + { + if (errno != EINTR) + { + throw defaultAllocator.make!BadLoopException(); + } + return; + } + + for (auto i = 0; i < eventCount; ++i) + { + auto transport = cast(StreamTransport) connections[events[i].data.fd]; + + if (transport is null) + { + auto connection = cast(ConnectionWatcher) connections[events[i].data.fd]; + assert(connection !is null); + + acceptConnections(connection); + } + else if (events[i].events & EPOLLERR) + { + kill(transport); + continue; + } + else if (events[i].events & (EPOLLIN | EPOLLPRI | EPOLLHUP)) + { + SocketException exception; + try + { + ptrdiff_t received; + do + { + received = transport.socket.receive(transport.output[]); + transport.output += received; + } + while (received); + } + catch (SocketException e) + { + exception = e; + } + if (transport.socket.disconnected) + { + kill(transport, exception); + continue; + } + else if (transport.output.length) + { + pendings.insertBack(transport); + } + } + if (events[i].events & EPOLLOUT) + { + transport.writeReady = true; + if (transport.input.length) + { + feed(transport); + } + } + } + } + + /** + * Returns: The blocking time. + */ + override protected @property inout(Duration) blockTime() + inout @safe pure nothrow + { + return min(super.blockTime, 1.dur!"seconds"); + } +} diff --git a/source/tanya/async/event/kqueue.d b/source/tanya/async/event/kqueue.d index 0fb4739..5ffa334 100644 --- a/source/tanya/async/event/kqueue.d +++ b/source/tanya/async/event/kqueue.d @@ -52,7 +52,7 @@ import core.stdc.errno; import core.sys.posix.time; // timespec import core.sys.posix.unistd; import core.time; -import std.algorithm.comparison; +import tanya.algorithm.comparison; import tanya.async.event.selector; import tanya.async.loop; import tanya.async.transport; @@ -217,7 +217,7 @@ final class KqueueLoop : SelectorLoop timespec ts; blockTime.split!("seconds", "nsecs")(ts.tv_sec, ts.tv_nsec); - if (changeCount > maxEvents) + if (changeCount > maxEvents) { events.length = changes.length; } diff --git a/source/tanya/container/array.d b/source/tanya/container/array.d index c2ac0ac..cd7aa1d 100644 --- a/source/tanya/container/array.d +++ b/source/tanya/container/array.d @@ -15,13 +15,14 @@ module tanya.container.array; import core.checkedint; -import std.algorithm.comparison; +import std.algorithm.comparison : equal; import std.algorithm.mutation : bringToFront, copy, fill, initializeAll, uninitializedFill; import std.meta; +import tanya.algorithm.comparison; import tanya.algorithm.mutation; import tanya.exception; import tanya.memory; diff --git a/source/tanya/container/list.d b/source/tanya/container/list.d index 81ce4d1..f7b13bb 100644 --- a/source/tanya/container/list.d +++ b/source/tanya/container/list.d @@ -15,8 +15,9 @@ */ module tanya.container.list; -import std.algorithm.comparison; +import std.algorithm.comparison : equal; import std.algorithm.searching; +import tanya.algorithm.comparison; import tanya.algorithm.mutation; import tanya.container.entry; import tanya.memory; @@ -707,7 +708,7 @@ struct SList(T) auto outOfScopeList = typeof(this)(allocator); outOfScopeList.head = *r.head; *r.head = null; - + return r; } diff --git a/source/tanya/container/string.d b/source/tanya/container/string.d index 690c74d..54c0200 100644 --- a/source/tanya/container/string.d +++ b/source/tanya/container/string.d @@ -26,9 +26,10 @@ */ module tanya.container.string; -import std.algorithm.comparison; +import std.algorithm.comparison : cmp, equal; import std.algorithm.mutation : bringToFront, copy; import std.algorithm.searching; +import tanya.algorithm.comparison; import tanya.algorithm.mutation; import tanya.memory; import tanya.meta.trait; diff --git a/source/tanya/math/mp.d b/source/tanya/math/mp.d index 8348998..b98fbd7 100644 --- a/source/tanya/math/mp.d +++ b/source/tanya/math/mp.d @@ -14,9 +14,10 @@ */ module tanya.math.mp; -import std.algorithm.comparison; +import std.algorithm.comparison : cmp, equal; import std.algorithm.mutation : copy, fill, reverse; import std.range; +import tanya.algorithm.comparison; import tanya.algorithm.mutation; import tanya.container.array; import tanya.encoding.ascii; @@ -1256,7 +1257,7 @@ struct Integer for (size_t i; i < this.size; ++i) { - const limit = min(factor.size, digits - i); + const limit = min(cast(size_t) factor.size, digits - i); word carry; auto k = i; diff --git a/source/tanya/memory/smartref.d b/source/tanya/memory/smartref.d index 5a97ebe..962752f 100644 --- a/source/tanya/memory/smartref.d +++ b/source/tanya/memory/smartref.d @@ -23,7 +23,7 @@ */ module tanya.memory.smartref; -import std.algorithm.comparison; +import tanya.algorithm.comparison; import tanya.algorithm.mutation; import tanya.conv; import tanya.exception; @@ -259,7 +259,7 @@ struct RefCounted(T) * reference types like classes, that can be accessed directly. * * Params: - * op = Operation. + * op = Operation. * * Returns: Reference to the pointed value. */ @@ -276,7 +276,7 @@ struct RefCounted(T) } /** - * Returns: Whether this $(D_PSYMBOL RefCounted) already has an internal + * Returns: Whether this $(D_PSYMBOL RefCounted) already has an internal * storage. */ @property bool isInitialized() const @@ -489,7 +489,7 @@ version (unittest) * A = Types of the arguments to the constructor of $(D_PARAM T). * allocator = Allocator. * args = Constructor arguments of $(D_PARAM T). - * + * * Returns: Newly created $(D_PSYMBOL RefCounted!T). * * Precondition: $(D_INLINECODE allocator !is null) @@ -743,7 +743,7 @@ struct Unique(T) * reference types like classes, that can be accessed directly. * * Params: - * op = Operation. + * op = Operation. * * Returns: Reference to the pointed value. */ @@ -837,7 +837,7 @@ struct Unique(T) * A = Types of the arguments to the constructor of $(D_PARAM T). * allocator = Allocator. * args = Constructor arguments of $(D_PARAM T). - * + * * Returns: Newly created $(D_PSYMBOL Unique!T). * * Precondition: $(D_INLINECODE allocator !is null) diff --git a/source/tanya/network/socket.d b/source/tanya/network/socket.d index d68378b..d016da6 100644 --- a/source/tanya/network/socket.d +++ b/source/tanya/network/socket.d @@ -16,10 +16,10 @@ module tanya.network.socket; import core.stdc.errno; import core.time; -import std.algorithm.comparison; public import std.socket : SocketOption, SocketOptionLevel; import std.traits; import std.typecons; +import tanya.algorithm.comparison; import tanya.memory; import tanya.os.error;